static map as a type function

Adam D. Ruppe destructionator at
Thu Sep 24 19:00:35 UTC 2020

Andrei beat me to the test implementation, it is good it kinda 

On Thursday, 24 September 2020 at 16:40:21 UTC, Stefan Koch wrote:
> You still have the problem of spamming the executable with 
> those type-info objects.

linker can strip that.

> And you have the problem of having to eagerly generate the 
> entire object.

Yeah, this is a potential major limitation: you can only use the 
data available there. But you could define your own classes that 
provide new data (can we have CTFE only classes as well as 
functions?!) as well as limit data you don't need.

Would be a new templated class instance per type which MIGHT 
runaway but not too bad.

But like you said in the other message the other issue is getting 
the original type T back out of the polymorphic typeinfo 
interface... if you need it. But maybe you could still have one 
template returning that and just using typeinfo for its internal 
intermediate calculations. (TypeInfo -> T can be done for a 
limited set by linear search. You cant pass a class instance as a 
template argument (alas... that restriction could *perhaps* be 
lifted in general tho), but one of the virtual methods could 
return a numeric id or string that is an index into a limited 
list of options.

class MyTypeInfo { immutable this(string name) { = 
name; } string name;}

template mytypeid(T) {
         static immutable MyTypeInfo store = new immutable 
         immutable(MyTypeInfo) mytypeid() { return store; }

template Tof(string name) {
         alias Tof = mixin(name);

immutable(MyTypeInfo) getTheString(immutable MyTypeInfo a, 
immutable MyTypeInfo b) {
         if(a is mytypeid!string)
                 return a;
         return b;

void main() {
         Tof!(mytypeid!(int).name) i;
         static assert(is(typeof(i) == int));
         i = 4;

         Tof!(getTheString(mytypeid!int, mytypeid!string).name) s;
         static assert(is(typeof(s) == string));

Obviously the stringof and mixin stuff does NOT work in general, 
as I talk about every chance I get (DON'T USE STRINGOF!!!!!), but 
the general approach here is plausible... at least given a 
limited set of options going into it.

More information about the Digitalmars-d mailing list