how to call class' template constructor

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 12 18:30:44 PDT 2014


On 10/12/14 3:46 PM, ketmar via Digitalmars-d-learn wrote:
> Hello.
>
> please, how to call template constructor of a class? it's completely
> escaped my mind. i.e. i have this class:
>
>    class A {
>      this(alias ent) (string name) {
>        ...
>      }
>    }
>
> and i want to do:
>
>    void foo () { ... }
>    auto a = new A!foo("xFn");
>
> yet compiler tells me that
>
> "template instance A!foo A is not a template declaration, it is a class"
>
> yes, i know that i can rewrite constructor to something like this:
>
>    this(T) (string name, T fn) if (isCallable!T) {
>      ...
>    }
>
> and then use autodeduction, but i want the first form! ;-)

Hm... I don't think it's very possible, unless you want to call ctor 
directly and not conflate with new (i.e. define a factory function 
instead). Unfortunately, some things can only be done within constructors.

At least someone else has found a similar issue before: 
https://issues.dlang.org/show_bug.cgi?id=10689

I'm not sure what proper syntax would be, perhaps:

auto a = (new A)!foo("xFn");

A good pattern should be able to be discovered here, to help with the 
existing state of affairs...

-Steve


More information about the Digitalmars-d-learn mailing list