D Language template


Example

An template can be introduced with template. It can contain functions and classes and other constructs.

template StaticArray(Type, size_t Length) {
     class StaticArray {
          Type content[Length];
          
          size_t myLength() {
            return getLength(this);
          }
     }
     
     private size_t getLength(StaticArray arr) {
          return Length;
     }
}

void main() {
    StaticArray!(int, 5) arr5 = new StaticArray!(int, 5);
    import std.stdio;
    writeln(arr5.myLength());
}