On Thursday, 18 September 2014 at 19:52:42 UTC, bearophile wrote:
IgorStepanov:
I've created pull request, which introduces multiple alias this.

Can someone show one or more usage cases?

Thank you,
bye,
bearophile

Do you ask about alias this or about it multiple usage. Multiple usage is similar to single, but multiple:)

struct Foo
{
    string s;
    int i;

    alias s this;
    alias i this;
}

Foo f = {"foo", 42};
string s = f; //s == "foo"
int i = f; //i == 42

If there are many different ways to resolve alias this then error is raised:

struct Bar
{
    double d;
    int i;

    alias d this;
    alias i this;
}

Foo f = {1.0, 42};
double d = f; //Error: compiler doesn't know, f.d or f.i do you want.

In the next expamle, compiler can resolve conflict:

struct Base1
{
    int i;

    alias i this;
}

struct Base2
{
    int i;

    alias i this;
}

struct Derived
{
    Base1 b1;
    Base2 b2;
    int i;

    alias b1 this;
    alias b2 this;
    alias i this;
}

Derived d = Derived(Base1(1), Base2(2), 3);
int i = d; //i == 3;
This done because Derived author know, how to cast his struct to int, and if he say alias i this; this alias hide aliases in base types.

However, if base type contains alias this to another acceptable type, and derived typ hasn't exactly castable alias this then error will be raised:

struct Base1
{
    short s;

    alias s this;
}

struct Base2
{
    int i;

    alias i this;
}

struct Derived
{
    Base1 b1;
    Base2 b2;
    int i;

    alias b1 this;
    alias b2 this;
    alias i this;
}

Derived d = Derived(Base1(1), Base2(2), 3);
int i = d; //Ok i == 3;
long l = d; //Error: what do you want? d.i or d.b1.s?

For additional info you can see examples in my pull request.

Reply via email to