On Monday, 25 May 2015 at 23:36:00 UTC, Timon Gehr wrote:
On 05/26/2015 01:17 AM, IgorStepanov wrote:


My example:
auto foo(T)(){ return 1; }
auto foo(T)()if(is(T: int)){ return 2; }

struct Foo
{
    //...
}
struct Bar
{
     Foo f;
     int i;
     alias i this;
}

auto ret = foo!Bar;
assert(ret == 2); //exactly the second function
...

(No, this actually raises an ambiguity error, but I see the point.)

After that, imagine, someone added alias intVal this to Foo.
Now there are many ways to convert Bar to int. However, user
don't know about it.

And if foo!Bar will start to return 1, it is very ugly situation.

I'm not convinced the alternative is better.

One can still do e.g.:

auto foo(T)()if(!is(typeof({static assert(is(T: int));}))){ return 1; } auto foo(T)()if(is(typeof({static assert(is(T: int));}))){ return 2; }
Yes, we will able hack the situation: for example via __traits(compile, ...) or via is(typeof(...)) However all those hacks are known and in strange template behaviour we able to find all those places.

I think, error is not bad. Multiple alias this is a new feature, and this solution allows us to catch all error and safely try this feature. Later we will able to relax all rules, when we will sure that it is safe.

Reply via email to