On Friday, 19 September 2014 at 05:52:53 UTC, deadalnix wrote:
On Thursday, 18 September 2014 at 11:20:49 UTC, IgorStepanov wrote:
I've created pull request, which introduces multiple alias this.
https://github.com/D-Programming-Language/dmd/pull/3998
Please see the additional tests and comment it.


What is the policy to resolve conflict ?


I wrote about that:

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?

Reply via email to