February 23, 2016
On Tuesday, 23 February 2016 at 12:43:42 UTC, Kagamin wrote:
> Don't we already have implicit conversions with alias this, so what's the deal?

The deal is you can't have implicit construction like:
A a = 5; // Error
a = 5 // okay

> struct A { int i; alias i this; }
> void f(int i){}
> void f(string s){}
> void g(A a){ f(a); } //what's called?

f(int) will be called. But problem here is the opposite: try calling g() with an int argument (like 5) and compiler won't let you.


import std.stdio;

struct A { int i; alias i this;}
void f(int i){writeln("int");}
void f(A a){writeln("A");}

void g(A a){ f(a); }

void main()
{
    g(A());
}

Above code prints "A". I would expect it to be an ambiguity error. Not sure if I should file a bug report?
February 23, 2016
On Tuesday, 23 February 2016 at 14:29:27 UTC, NX wrote:
> The deal is you can't have implicit construction like:
> A a = 5; // Error

That is *explicit* construction and it is perfectly legal in D.

It is just done with constructors, not alias this. Alias this has absolutely nothing to do with construction, it plays zero role.

All alias this does is forward to a member object when you try to use it as a case where the outer type doesn't work but the inner type does. The object must already exist for this to happen, so it can't work on constructors.

> import std.stdio;
>
> struct A { int i; alias i this;}
> void f(int i){writeln("int");}
> void f(A a){writeln("A");}
>
> void g(A a){ f(a); }
>
> void main()
> {
>     g(A());
> }
>
> Above code prints "A". I would expect it to be an ambiguity error. Not sure if I should file a bug report?

There's no ambiguity there and no bug; it is working as defined. alias this is only ever invoked if the outer type doesn't fit. It does fit here, A is A, so no need to check alias this at all.
February 23, 2016
On Tuesday, 23 February 2016 at 14:35:35 UTC, Adam D. Ruppe wrote:
> There's no ambiguity there and no bug; it is working as defined. alias this is only ever invoked if the outer type doesn't fit. It does fit here, A is A, so no need to check alias this at all.

It's arguably the right design. Conservativeness can prevent a lot of stupidity. I don't have a an example at hand though...
February 23, 2016
On Tuesday, 23 February 2016 at 15:07:08 UTC, NX wrote:
> It's arguably the right design.

When I say arguably I don't mean arguably :D

"It's not the right design in my opinion"
1 2
Next ›   Last »