static map as a type function

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Thu Sep 24 16:41:26 UTC 2020


Actually I just realized no need to rewrite the map algorithm. Given 
that we're dealing with bona fide objects, all algorithms in std work 
out of the box. This compiles and runs:

https://run.dlang.io/is/TprA9u

==========================

import std;

// Base class for CT and RT type information.
immutable abstract class NewTypeInfo {
     size_t tsize();
}

// Implementation for given type T.
immutable class NewTypeInfoImpl(T) : NewTypeInfo {
     override size_t tsize() {
         return T.sizeof;
     }
}

// Use __typeid!T to get a singleton object associated with type T.
@property immutable(NewTypeInfo) __typeid(T)() {
     static immutable singleton = new NewTypeInfoImpl!T;
     return singleton;
}

static assert([__typeid!int, __typeid!ushort].map!(t => 
t.tsize).equal([4, 2]));

void main() {}



More information about the Digitalmars-d mailing list