A Tour of Morfa

Const templates

A const template is a value that is parametrized with type.

import advanced.templates.class_templates;
import advanced.templates.function_templates;

template <T>
const nil: List<T> = null;

An instance of nil for int is like null but carries type information:

unittest
{
    assert (length(nil<int>) == 0);    
}

A const template with two type parameters may encode a relation between types. For example:

template <T, U>
const IsNumericSubtypeOf = T.min >= U.min and T.max <= U.max;

static assert (IsNumericSubtypeOf<int8, int16>);
static assert (not IsNumericSubtypeOf<int16, int8>);
static assert (IsNumericSubtypeOf<float32, float64>);