A Tour of Morfa

(Templated) Conversions

An important detail of making the previous concepts work for the matrix DSL are user-defined conversions.

These follow a fairly C++-like convention, but are granted a very transparent and obvious syntax, as you will see.

One of the conversions which are quite natural in the matrix world are coercions of single figures to matrices, like this one:

var a: Matrix = 5;

Assuming that we have a way to construct a Matrix object from a numeric scalar, we can provide this to the user elegantly:

template <T>
if (IsNumberType<T>)
public func convert(d: T): Matrix
{
    return new Matrix(d); 
    // or "return Matrix(d);" if Matrix is a struct
}

Other point where conversions come in handy is the indexing. We usually want to be able to slice matrices with matrices as in var A = ᴹ[0, 1, 2]; B[A, ..]; (see Section ᴹ, | and [] for explanation of the ).

To have this functionality, add a simple conversion:

public func convert(m: Matrix): SomeGenericRangeType;

following the convention from this section.