static map as a type function
Stefan Koch
uplink.coder at googlemail.com
Thu Sep 24 16:46:21 UTC 2020
On Thursday, 24 September 2020 at 16:41:26 UTC, Andrei
Alexandrescu wrote:
> 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() {}
Now all you have to do is to be able to use the type member of
the typeid template upon returning from the function.
And you're at feature parity with type functions.
That however requires a TOP-type container, which is alias[] :)
More information about the Digitalmars-d
mailing list