Is there a better way to do this? (Using class at compile-time)

bauss jj_1337 at live.dk
Tue Jun 12 11:04:40 UTC 2018


Let's say I have something like the following:

abstract class Foo
{
     abstract string baz();
     abstract string helloworld();
}

final class Bar(T) : Foo
{
     override string baz() { return "writeln(\"Hello " ~ 
T.stringof ~ "!\");"; }
     override string helloworld() { return "writeln(\"Hello 
World!\");"; }
}

auto getBar(T)() { return new Bar!T; }

void test(T)()
{
     pragma(msg, (getBar!T).baz());
     pragma(msg, (getBar!T).helloworld());

     import std.stdio;

     mixin((getBar!T).baz());
     mixin((getBar!T).helloworld());
}

void main()
{
     test!int;
}

Is there a way to avoid having to write "getBar!T" every time 
since we cannot store a class into a compile-time variable, how 
would one avoid such thing?

The best I can think of is an alias like:

void test(T)()
{
     alias bar = getBar!T;

     pragma(msg, bar.baz());
     pragma(msg, bar.helloworld());

     import std.stdio;

     mixin(bar.baz());
     mixin(bar.helloworld());
}

But I'm positive there must be a better way.


More information about the Digitalmars-d-learn mailing list