UFCS in the presence of alias this

Jens Mueller jens.k.mueller at gmx.de
Thu Apr 19 02:28:09 PDT 2012


Hi,

using UFCS with alias this behaves strange.
Consider

struct Foo
{
    int _member;
    alias _member this;
}

int foo(Foo f) { return f._member; }
int foo(int i) { return i; }

unittest
{
    Foo f;
    f.foo(); // which function will be called? Isn't it ambiguous?
}

Due to the alias this the function foo(int i) will be called. Is this
the indented behavior? Practically, I want UFCS to just perform a
syntactic rewrite from f.foo() to foo(f).
When using alias this you have to define the second function. Providing
only the first one results in a compile error. If you remove the alias
this things work as expected (first one needs to be defined).

I stumbled upon this problem when trying to define additional functions
for a Tuple. Tuple has "alias field this" for falling back on
TypeTuple's opIndex. Unfortunately,

alias Tuple!(int) Bar;
int bar(typeof(Bar.field) b) { return 1; }

unittest
{
    Bar b;
    b.bar();
}

does not work.

Jens


More information about the Digitalmars-d mailing list