23: Templates - royal-lang/rl GitHub Wiki
Templates are Royal's answer to generics. Templates are much more than just types, they can be used to create dynamic and very abstract compile-time concepts.
Templates in Royal can take both types, aliases and compile-time available values.
template NAME(TEMPLATE_PARAMS)
{
...
}
fn NAME(TEMPLATE_PARAMS)(PARAMS)
{
...
}
struct NAME(TEMPLATE_PARAMS)
{
...
}
ref struct NAME(TEMPLATE_PARAMS)
{
...
}
interface NAME(TEMPLATE_PARAMS)
{
...
}
trait NAME(TEMPLATE_PARAMS)
{
...
}
Example:
template TCopy(T)
{
fn copy(T source, out T destination)
{
destination = source;
}
}
var x = 10;
var int y;
TCopy(int).copy(x, out y);
Is the same as:
fn copy(T)(T source, out T destination)
{
destination = source;
}
var x = 10;
var int y;
copy(int)(x, out y);
Royal can also interfere the template types based on the given parameters to a function call.
Example:
fn copy(T)(T source, out T destination)
{
destination = source;
}
var x = 10;
var int y;
copy(x, out y); // (T) is interfered as (int)