Thread overview
how to call class' template constructor
Oct 12, 2014
ketmar
Oct 12, 2014
JR
Oct 12, 2014
ketmar
Oct 12, 2014
Cliff
Oct 12, 2014
anonymous
Oct 13, 2014
ketmar
October 12, 2014
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! ;-)


October 12, 2014
On Sunday, 12 October 2014 at 19:46:41 UTC, 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");

Template the whole class? class A(alias ent)?
October 12, 2014
On Sunday, 12 October 2014 at 19:46:41 UTC, 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! ;-)

How about a static factory method?  Or do you know there is a syntax for invoking a templatized constructor and just can't remember it?
October 12, 2014
On Sunday, 12 October 2014 at 19:46:41 UTC, 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"

I think there's no nice way to explicitly pass template arguments
to constructors. Here's a hack:

   auto a = cast(A) (new void[__traits(classInstanceSize, A)]).ptr;
   a.__ctor!foo("xFn");

I didn't think this through or test it thoroughly. So it most
probably has issues. You could try to identify and fix them, and
then hide the ugly away in a function.

But the two leading underscores tell it: "__ctor" is an
implementation detail. It may disappear/change without notice.

Some other solution/workaround would probably be the better
choice. Like a static factory method that's already been
mentioned.
October 12, 2014
On Sun, 12 Oct 2014 20:29:39 +0000
JR via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> Template the whole class? class A(alias ent)?

no, that will produce different classes for different types, and i need just one class, but initialized with different entities. with templated class i need to make A ancestor of some BaseA to make checks with `cast(...)`.


On Sun, 12 Oct 2014 20:35:52 +0000
Cliff via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> How about a static factory method?
i still need to call templated constructor with it. and i want to init some immutable fields in it, and that fields depends of entity. sure, i can write alot of non-templated constructors and call 'em from factory method, or make other changes, but it leads to code bloat. i want to avoid id.

> Or do you know there is a syntax for invoking a templatized constructor and just can't remember it?
no, i'm not even sure that such syntax exists. i always used templated constructors with autodeduction.


On Sun, 12 Oct 2014 20:49:29 +0000
anonymous via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> I think there's no nice way to explicitly pass template arguments to constructors. Here's a hack:
i know about hackish way, but it's SOOO UUUGLY... ;-) thank you anyway.

> But the two leading underscores tell it: "__ctor" is an implementation detail. It may disappear/change without notice.
ah, that's ok, i'm relying on other implementation details too, one more will not make much difference.


thank you people. seems that there is no "official" way to call such constructors. ok, i converted it to autodeducing one, one more argument doesn't really matter, it's hidden in another template anyway.

i was just curious if i missed some nice syntax here, or we have no way to do this without resorting to hack or factory function.


October 13, 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
October 13, 2014
On Sun, 12 Oct 2014 21:30:44 -0400
Steven Schveighoffer via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

> At least someone else has found a similar issue before: https://issues.dlang.org/show_bug.cgi?id=10689
hm. i somehow missed this in my searches. thanks.

> I'm not sure what proper syntax would be, perhaps:
> auto a = (new A)!foo("xFn");
how about: `auto a = new A.this!foo("xFn");`?

there is no harm in specifying constructor name for resolving ambiguity here. and it still allows `auto a = new A!int.this!foo("xFn");` for example. and it's not breaking any existing code, afaik.

i'm sure that this is not a complex patch. let's hope someone will write it and convince Walter to accept it.