Thread overview
mixed-in ctor not on par with explicit one?
Jan 13, 2016
Shriramana Sharma
Jan 13, 2016
Jacob Carlborg
Jan 13, 2016
Shriramana Sharma
Jan 13, 2016
Jacob Carlborg
Jan 13, 2016
Daniel N
Jan 13, 2016
Adam D. Ruppe
Jan 13, 2016
Shriramana Sharma
January 13, 2016
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?

-- 
Shriramana Sharma, Penguin #395953
January 13, 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
January 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?

-- 
Shriramana Sharma, Penguin #395953

January 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
January 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 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');
}

January 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

-- 
Shriramana Sharma, Penguin #395953
January 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.