Skip to content
Matthew edited this page Jul 4, 2018 · 4 revisions

Jet supports a limited form of Templates that is more similar to generics. Unlike in C++ you have to specify a trait that you want your generic argument to match when declaring templates. This allows you to have greater type safety and avoid messy compilation errors by erroring early on before instantiation.

You declare a struct or function template as follows:

// Any is the trait name, T is the template variable name
struct MyStruct<Any T>
{
  T val;
}

fun T DoThing<Any T>(T arg)
{
  return arg;
}

To instantiate a template struct just add < followed by your type name and > after it as shown below:

let MyStruct<int> v;
v.val = 5;

Template argument inference is supported so you do not always have to specify a type when calling a templated function:

let int i = 5;
let int r = DoThing(i);
Clone this wiki locally