traits: how to split parametrized type into basic type and parameters

Martin Kinkelin noone at spam.com
Sun Feb 13 15:17:39 PST 2011


Hi,

I'm implementing a generic Vector!(uint d, T) struct (wrapping a T[d] array).
For readability:
alias Vector!(4u,float) float4;
alias Vector!(4u,int)   int4;

I'd like to be able to cast a float4 variable explicitly to an int4
(component-wise casting):
auto f4 = float4(1,2,3,4);
auto i4 = cast(int4) f4;

So when implementing opCast!NewType() in Vector!(d,T), I need a signature
constraint: NewType must be a Vector!(d2,T2), with d2 == d && isNumeric!T2.
The problem is that I don't know how to split NewType into the basic type
(Vector) and its parameters ({uint d2, T2}) as I didn't find anything related
in the std.traits documentation. I currently use a custom casting method, but
am not happy with it (I prefer the clean casting syntax preceding the expression):
Vector!(d,T2) castTo(T2)() if (isNumeric!T2) { ... }
// e.g., auto i4 = f4.castTo!int();

Thanks in advance for any hints. Already falling in love with D after 2 days
of experimenting :)


More information about the Digitalmars-d-learn mailing list