Disappointing inflexibility of argument passing with "alias this"
NX via Digitalmars-d
digitalmars-d at puremagic.com
Tue Feb 23 06:29:27 PST 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?
More information about the Digitalmars-d
mailing list