digitalmars.D.learn - mixed-in ctor not on par with explicit one?
- Shriramana Sharma (14/14) Jan 12 2016 Hello. Compiling the following code:
- Jacob Carlborg (8/20) Jan 13 2016 Mixed in functions are in a different overload set from the context
- Shriramana Sharma (34/37) Jan 13 2016 But I'm not able to do that with `this`:
- Jacob Carlborg (5/7) Jan 13 2016 Looks like a limitation in the language.
- Daniel N (20/25) Jan 13 2016 This works:
- Adam D. Ruppe (13/14) Jan 13 2016 Indeed... but interestingly, it does not work if you define the
- Shriramana Sharma (5/6) Jan 13 2016 Apparently already reported as well:
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
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
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
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
On Wednesday, 13 January 2016 at 12:39:36 UTC, Jacob Carlborg wrote:On 2016-01-13 10:48, Shriramana Sharma wrote: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'); }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.
Jan 13 2016
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
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