Eigenpoll : D wish list : Better Array Function Template 

AFAIK there is no way of writing a template function that does essentially the same thing to a regular array and an associative array with implicit instantiation. For example, let's say I wanted to write a simple extract column function for a 2-d array. For a regular array, it might be:

T[] extract_column(T)(T[][] data, size_t column)
{
T[] result;
foreach(row; data)
{
result~=row[column];
}
return result;
}

However, for an associative array, it would have to be:

T[U] extract_column(T, U, V)(T[U][V] data, U column)
{
T[U] result;
foreach(row; data)
{
result~=row[column];
}
return result;
}

The other option would be to require explicit instantiation, but something like this should be doable using implicit instantiation from function parameter types.

The root of the problem is that syntactially, regular arrays are T[no type]. Since D has strong typedefs, it might work to define a regular array as T[size_t], since this would be different from T[int] or T[uint], which is associative.


Report this item for cleanup