Generics

Note that the exact syntax is as of set TBD. The general syntactic idea will remain the same, but there may be some differences in the details. Furthermore, (D-like) templates may be provided instead.

Generics are available: Types parameterized by other types. Support for value parameters is unlikely, but it is a possibility. Exact details are unknown, but they will likely by design be kept *non-*turing-complete.

// LinkedListNode#T is shorthand for LinkedListNode#(T), as multiple parameters are supported
class LinkedListNode#T
{
    T item;
    LinkedListNode#T? next = null;
    // constructor
    this(T item) { this.item = item; }
}

// function example
void swap#T(ref T a, ref T b)
{
    T tmp = a;
    a = b;
    b = tmp;
    // or: (if tuples are ever added)
    //(a, b) = (b, a);
}

// another function example
void doStuff#T(T a)
{
    // Illegal! The only supported operation is assignment. We need to specify traits to get more, or use a `dynamic` type.
    a.foo = 3;
}

// all 3 are equivalent (because in this instance, the type can be inferred)
var node = new LinkedListNode#int(3);
var node = new LinkedListNode#(int)(3);
var node = new LinkedListNode(3);

The reason the Foo<T> syntax is not used is primarily that it creates a complicated scenario where >> and >>> must properly be handled in the lexer depending on the state in the parser. This is because examples such as Map<string,Array<int>> needing special lexical treatment. In other words, it’s a nightmare to parse.

I am not a great fan of Foo#T visually, however. D uses Foo!T to great effect, but D is unburdened by the postfix ! operator. Here are a few syntactic possibilities:

//Array<int>, Map<string,int>	// not doing this one, but included as reference
// Most of these would also work well as `Map#[string, int]` or `Map#{string, int}`; omitted for brevity.
Array#int, Map#(string, int)
Array$int, Map$(string, int)	// might be the best option?
Array@int, Map@(string, int)
Array\int, Map\(string, int)
Array{int}, Map{string, int}	// doesn't look bad, but it might be ambiguious?

It is desired to support variadic generics, although the syntax & semantics are TBD. But here is one example:

string format#(Args...)(string fmt, Args args)
{
    // don't like this syntax, but just to provide the idea
    static foreach(i, arg : args)	// type of `arg` is populated correctly!
        ...
}

Since this is a scripting language, perhaps the following would be adequate, however:

string format(string fmt, dynamic[] args...)
{
    ...
}