www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixed-in ctor not on par with explicit one?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Hello. Compiling the following code:

mixin template intCtor() { this(int i) {} }
struct Test { mixin intCtor; this(string s) {} }
void main()
{
    auto a = Test("hello");
    auto b = Test(1);
}

...gives the error:

<src>(6): Error: constructor <src>.Test.this (string s) is not callable 
using argument types (int)

What is the problem in calling the mixed-in ctor?

-- 

Jan 12 2016
parent reply Jacob Carlborg <doob me.com> writes:
On 2016-01-13 04:32, Shriramana Sharma wrote:
 Hello. Compiling the following code:

 mixin template intCtor() { this(int i) {} }
 struct Test { mixin intCtor; this(string s) {} }
 void main()
 {
      auto a = Test("hello");
      auto b = Test(1);
 }

 ...gives the error:

 <src>(6): Error: constructor <src>.Test.this (string s) is not callable
 using argument types (int)

 What is the problem in calling the mixed-in ctor?
Mixed in functions are in a different overload set from the context where they're mixed in [1]. [1] http://dlang.org/spec/template-mixin.html - search for "Alias declarations can be used to overload together functions declared in different mixins" -- /Jacob Carlborg
Jan 13 2016
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Hello and thanks for your reply.

Jacob Carlborg wrote:

 [1] http://dlang.org/spec/template-mixin.html - search for "Alias
 declarations can be used to overload together functions declared in
 different mixins"
But I'm not able to do that with `this`: mixin template myCtors() { this(int i) {} this(char c) {} } struct Test { mixin myCtors; alias this = myCtors.this; this(string s) {} } void main() { auto a = Test("hello"); auto b = Test(1); auto c = Test('c'); } But I get the error: <src>(9): Error: identifier expected following '.', not 'this' <src>(9): Error: cannot use syntax 'alias this = myCtors', use 'alias myCtors this' instead Even actually giving the mixin instance an identifier doesn't help: mixin myCtors!() myCtorsInst; alias this = myCtorsInst.this; <src>(9): Error: identifier expected following '.', not 'this' <src>(9): Error: cannot use syntax 'alias this = myCtorsInst', use 'alias myCtorsInst this' instead This is not what alias <> this is supposed to do, right? So how am I supposed to get the mixed in ctors work? --
Jan 13 2016
parent reply Jacob Carlborg <doob me.com> writes:
On 2016-01-13 10:48, Shriramana Sharma wrote:

 This is not what alias <> this is supposed to do, right?
No.
 So how am I supposed to get the mixed in ctors work?
Looks like a limitation in the language. -- /Jacob Carlborg
Jan 13 2016
next sibling parent reply Daniel N <ufo orbiting.us> writes:
On Wednesday, 13 January 2016 at 12:39:36 UTC, Jacob Carlborg 
wrote:
 On 2016-01-13 10:48, Shriramana Sharma wrote:

 This is not what alias <> this is supposed to do, right?
No.
 So how am I supposed to get the mixed in ctors work?
Looks like a limitation in the language.
This works: mixin template myCtors() { this(int i) {} this(char c) {} } struct Test { this(string s) {} mixin myCtors mixed; alias __ctor = mixed.__ctor; } void main() { auto a = Test("hello"); auto b = Test(1); auto c = Test('c'); }
Jan 13 2016
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 13 January 2016 at 13:53:01 UTC, Daniel N wrote:
 This works:
Indeed... but interestingly, it does not work if you define the mixin before the new constructor: struct Test { mixin myCtors mixed; alias __ctor = mixed.__ctor; this(string s) {} } l.d(10): Error: alias l.Test.__ctor is not a constructor; identifiers starting with __ are reserved for the implementation So yeah, you can make it work, but there do appear to be a few bugs with it anyway.
Jan 13 2016
prev sibling parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Jacob Carlborg wrote:

 Looks like a limitation in the language.
Apparently already reported as well: https://issues.dlang.org/show_bug.cgi?id=11500 --
Jan 13 2016