TypeFunction example creatiing a conversion matrix

Stefan Koch uplink.coder at gmail.com
Thu Oct 1 12:27:23 UTC 2020


On Thursday, 1 October 2020 at 11:35:10 UTC, jmh530 wrote:
> On Thursday, 1 October 2020 at 09:54:25 UTC, Stefan Koch wrote:
>> [snip]
>
> I would imagine you should also be able to do it like:
>
> T max(alias T, T a, T b) {
>     return (a > b) ? a : b;
> }
> float gimmeTheBiggerFloat(float a, float b) {
>     return max(float, a, b);
> }
> uint gimmeTheBiggerInteger(uint a, uint b) {
>     return max(uint, a, b);
> }
>

No you would not be able to write that.
Because type functions are not polymorphic.
I.E. you cannot declare a variable with a type alias.

type functions do a Type -> TypeObject conversion when you pass 
the types in.
inside the type function you get a "type-like" interface.
But it has lost everything that made it a type.
It has sizeof, alignof, stringof, tupleof, you can use __traits 
on it.
But it cannot be used as a type anymore.
Not inside a type-function anyway.

If you return a type or a type tuple from a type function, it 
becomes a regular type/type tuple again.



More information about the Digitalmars-d mailing list