Difference between revisions of "DIP66"

From D Wiki
Jump to: navigation, search
(Limitations)
(Resolution Algorithm)
(9 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
|-
 
|-
 
|Version:
 
|Version:
|1.1
+
|1.2
 
|-
 
|-
 
|Status:
 
|Status:
Line 28: Line 28:
 
== Abstract ==
 
== Abstract ==
  
An AliasThis declaration names a member to subtype.  Multiple AliasThis declarations are allowed.  Order of AliasThis declarations does not matter.
+
An ''AliasThis'' declaration names a member to subtype.  Multiple ''AliasThis'' declarations are allowed.  Order of ''AliasThis'' declarations does not matter.
  
 
==Description==
 
==Description==
Line 40: Line 40:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
... the construction <code>alias ''symbol'' this;</code> means that wherever <code>typeof(Foo.symbol)</code> is needed, <code>obj</code> (object of type <code>Foo</code>) can be substituted with <code>obj.symbol</code>.
+
... the construction <code>alias ''symbol'' this;</code> means that wherever <code>typeof(Foo.symbol)</code> is needed, <code>obj</code> (object of type <code>Foo</code>) can be substituted with <code>obj.symbol</code>. Effectively the construct declares <code>typeof(Foo.symbol)</code> as a supertype of <code>Foo</code>, with the implicit conversion dictated by <code>obj.symbol</code> (which may be a <code>static</code> member variable, a direct member variable, or a method).
This rule applies to implicit and explicit conversion, <code>.member</code> access expression, operator overloading, foreach expression (<code>foreach(args; ''obj'')</code>) etc.
+
<code>''symbol''</code> can be an any symbol when obj.symbol is a valid expression.
+
This rule applies in all instances where subtyping applies: implicit and explicit conversion, <code>.member</code> access expression, operator overloading, foreach expressions (<code>foreach(args; ''obj'')</code>) etc.
If more than one <code>alias this</code> can be used to solve the same lookup,  the compiler should raise an error.
+
<code>''symbol''</code> can be an any symbol when <code>obj.symbol</code> is a valid expression.
 +
 
 +
If more than one <code>alias this</code> can be used to solve the same lookup,  an error is raised during compilation.
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 76: Line 78:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Alias this and l-values===
+
===<code>alias this</code> and l-values===
  
As mentioned above, alias this symbol may be a field (which is l-value) or method (which may be r-value).
+
As mentioned above, the <code>alias this</code> symbol may be a field (which is an l-value) or method (which may be an r-value).
Subtyped struct may be passed to a function as a ref and used as a l-value if its alias this symbol is l-value.
+
Subtyped <code>struct</code> values may be passed to a function as a <code>ref</code> and used as an l-value if its alias this symbol is l-value.
When called function is overloaded and may take r-value and l-value argument, compiler prefer l-value if alias this symbol is l-value.
+
When the called function is overloaded and may take r-value and l-value argument, the l-value is preferred if alias this symbol is an l-value.
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 114: Line 116:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
However, when type D can be converted to B in several ways and if a first way gets a l-value but a second way gets a r-value and if D passed to function foo which takes l-value, compiler will not prefer l-value path and will raise an error:
+
However, when type <code>D</code> can be converted to <code>B</code> through several paths and one yields an l-value whereas the other yields an r-value, the code is in error regardless of context. Example:
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 149: Line 151:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This is done because <code>alias this</code> provides subtyping and A and B have the same subtype: int. L-value modifier is not a part of type and statement "A is a subtype of l-value int" doesn't make sense. "A is a subtype of int" is correct assertion.
+
This is done because <code>alias this</code> provides subtyping and <code>A </code>and <code>B</code> have the same subtype: <code>int</code>. L-value modifier is not a part of type and statement "<code>A</code> is a subtype of l-value <code>int</code>" doesn't make sense. "<code>A</code> is a subtype of <code>int</code>" is correct assertion.
  
 
===Method overloading===
 
===Method overloading===
  
There are two important cases of overloading: foo(X) tries to overload base type foo(Y) and basetype2.foo(X) tries to overload basetype2.foo(Y).
+
There are two important cases of overloading: <code>foo(X)</code> tries to overload base type <code>foo(Y)</code> and <code>basetype2.foo(X)</code> tries to overload <code>basetype2.foo(Y)</code>.
  
At the first case semantic rule says: "Derived type methods hides base type methods."
+
At the first case semantic rule says: "Derived type methods hide base type methods."
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 180: Line 182:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
At the second case semantic rule says: "When parameter set can be applied only to one base type overloaded method, compiler will choose it. However, if parameter set can be applied to several base type overloaded methods (even if one matching is better than others), compiler should raise an error."
+
The semantic rule for the second cast prescribes: "When parameter set can be applied only to one base type overloaded method, compiler will choose it. However, if parameter set can be applied to several base type overloaded methods (even if one matching is better than others), compiler should raise an error."
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 222: Line 224:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Semantics==
+
==Resolution Algorithm==
  
 
Multiple <code>alias this</code> can cause conflicts. This section explains how the compiler should resolve them.
 
Multiple <code>alias this</code> can cause conflicts. This section explains how the compiler should resolve them.
At the AliasThis declaration semantic stage, the compiler can perform the initial checks and reject the obviously incorrect AliasThis declarations.
+
At the ''AliasThis'' declaration semantic stage, the compiler can perform the initial checks and reject the obviously incorrect ''AliasThis'' declarations.
  
 
<syntaxhighlight lang=D>
 
<syntaxhighlight lang=D>
Line 258: Line 260:
 
More precisely, this is the order in which <code>obj.xyz</code> is looked up:
 
More precisely, this is the order in which <code>obj.xyz</code> is looked up:
  
# If <code>xyz</code> is a symbol (member, method, <code>enum</code> etc) defined inside <code>typeof(obj)</code> then lookup is done.
+
# If xyz is a symbol (member, method, enum etc) defined inside typeof(obj) then lookup is done.
# Otherwise, if <code>xyz</code> is a symbol introduced in the base class (where applicable), then lookup is done.
+
# Otherwise, if xyz is a symbol introduced in the base class (where applicable), then lookup is done.
# Otherwise, if <code>opDispatch!"xyz"</code> exists, then lookup is done.
+
# Otherwise, if xyz is found at least via either an opDispatch!"xyz" or alias this conversion, then lookup is done.
# Otherwise, <code>alias this</code> is attempted transitively, and if <code>xyz</code> is found, then lookup is done.
 
 
# Otherwise an UFCS rewrite is effected.
 
# Otherwise an UFCS rewrite is effected.
  

Revision as of 21:56, 25 May 2015

Title: (Multiple) alias this
DIP: 66
Version: 1.2
Status: Approved contingent to these amendments
Created: 2014-10-09
Last Modified: 2014-11-2
Author: Igor Stepanov
Links:

Abstract

An AliasThis declaration names a member to subtype. Multiple AliasThis declarations are allowed. Order of AliasThis declarations does not matter.

Description

In the code below...

    struct Foo
    {
        //...
        alias symbol this;
    }

... the construction alias symbol this; means that wherever typeof(Foo.symbol) is needed, obj (object of type Foo) can be substituted with obj.symbol. Effectively the construct declares typeof(Foo.symbol) as a supertype of Foo, with the implicit conversion dictated by obj.symbol (which may be a static member variable, a direct member variable, or a method).

This rule applies in all instances where subtyping applies: implicit and explicit conversion, .member access expression, operator overloading, foreach expressions (foreach(args; obj)) etc. symbol can be an any symbol when obj.symbol is a valid expression.

If more than one alias this can be used to solve the same lookup, an error is raised during compilation.

    struct A
    {
        int i;
        alias i this;
    }

    struct B
    {
        int i;
        alias i this;
    }

    struct C
    {
        A a;
        B b;

        alias a this;
        alias b this;
    }
    
    void test()
    {
        C c;
        int i = c; //Error: c.a.i vs c.b.i
    }
    
    static assert(is(C : int)); //Error: c.a.i vs c.b.i

alias this and l-values

As mentioned above, the alias this symbol may be a field (which is an l-value) or method (which may be an r-value). Subtyped struct values may be passed to a function as a ref and used as an l-value if its alias this symbol is l-value. When the called function is overloaded and may take r-value and l-value argument, the l-value is preferred if alias this symbol is an l-value.

    struct A
    {
        int a;
        alias a this;
    }

    struct B
    {
        int foo() { return 1; };
        alias foo this;
    }

    int testX(ref int x)
    {
        return 1;
    }

    int testX(int x)
    {
        return 2;
    }

    void test()
    {
        A a;
        B b;
        assert(testX(a) == 1); //a.a is l-value
        assert(testX(b) == 2); //b.foo is r-value
    }

However, when type D can be converted to B through several paths and one yields an l-value whereas the other yields an r-value, the code is in error regardless of context. Example:

    struct A
    {
        int a;
        alias a this;
    }

    struct B
    {
        int foo() { return 1; };
        alias foo this;
    }

    struct C
    {
        A a;
        B b;
        alias a this;
        alias b this;
    }

    int testX(ref int x)
    {
        return 1;
    }

    void test()
    {
        C c;
        testX(c); //Error: multiple ways to convert C to int: C.a.a and C.b.foo
    }

This is done because alias this provides subtyping and A and B have the same subtype: int. L-value modifier is not a part of type and statement "A is a subtype of l-value int" doesn't make sense. "A is a subtype of int" is correct assertion.

Method overloading

There are two important cases of overloading: foo(X) tries to overload base type foo(Y) and basetype2.foo(X) tries to overload basetype2.foo(Y).

At the first case semantic rule says: "Derived type methods hide base type methods."

    struct A
    {
        int foo(int) { return 1; }
        int foo(string) { return 1; }
    }

    struct B
    {
        int foo(double) { return 3; };
        A a;
        alias a this;
    }

    void test()
    {
        B b;
        b.foo(2.0);      //Ok, call B.foo(double);
        b.foo(2);        //Ok, call B.foo(double); A.foo(int) is hidden
        b.foo("string"); //Error, unable to convert string to double. A.foo(string) is hidden
    }

The semantic rule for the second cast prescribes: "When parameter set can be applied only to one base type overloaded method, compiler will choose it. However, if parameter set can be applied to several base type overloaded methods (even if one matching is better than others), compiler should raise an error."

    struct A
    {
        char foo(int)
        {
            return 'I';
        }
    }

    struct B
    {
        char foo(string)
        {
            return 'S';
        }

        double foo(double)
        {
            return 'D';
        }
    }

    struct C
    {
        A a;
        B b;
        alias a this;
        alias b this;
    }

    void test()
    {
        C c;
        assert(c.foo("string") == 'S'); //Ok. Only c.b.foo(string) is matching.
        assert(c.foo(1.2) == 'D');      //Ok. Only c.b.foo(double) is matching.
        c.foo(1);                       //Error: there are two base methods may be used: c.b.foo(double) and c.a.foo(int)
                                        //No matter that c.a.foo(int) is matches better.
    }

Resolution Algorithm

Multiple alias this can cause conflicts. This section explains how the compiler should resolve them. At the AliasThis declaration semantic stage, the compiler can perform the initial checks and reject the obviously incorrect AliasThis declarations.

    struct Test1
    {
        int a;
        int b;
        alias a this;
        alias b this; // Error: alias b this conflicts with alias a this;
    }

    class Test2a
    {
    }

    class Test2b : Test2a
    {
    }

    class Test2 : Test2b
    {
        Test2a a;
        alias a this; //Error: alias a this tries to hide inherited type Test2a; 
    }

The other checks will be done when alias this is needed for typing expressions. When the compiler types an expression such as fun(a), it can resolve it as fun(a.aliasThisSymbol). (Hereinafter fun(a) means any case when alias this can be used: type conversion, .member expression, operator expression etc.) However compiler will try fun(a.aliasThisSymbol) only if the expression cannot be typed otherwise.

More precisely, this is the order in which obj.xyz is looked up:

  1. If xyz is a symbol (member, method, enum etc) defined inside typeof(obj) then lookup is done.
  2. Otherwise, if xyz is a symbol introduced in the base class (where applicable), then lookup is done.
  3. Otherwise, if xyz is found at least via either an opDispatch!"xyz" or alias this conversion, then lookup is done.
  4. Otherwise an UFCS rewrite is effected.

When the compiler is trying to resolve alias this it iterates all alias this declarations and tries to apply each. For each successful application, the compiler adds the result expression into the result set. If application fails, the compiler tries to recursively resolve the alias this expression. Also, if our type is a class, compiler tries to recursively resolve all inherited types. Finally, if resultSet contains only one candidate, the compiler will accept it. Otherwice, if resultSet is empty, compiler tries another ways to resolve ex(obj): UFCS et c. Otherwice, if resultSet contains more then one candidates, the compiler raises an error.

Recursive alias this may occur:

    class A
    {
        C c;
        alias c this;
    }

    class B
    {
        A a;
        alias a this;
    }

    class C
    {
        B b;
        alias b this;
    }


For resolving this situation, the resolveAliasThis function stores a set of types (visitedTypes), which can be visited higher in the call stack. If visitedTypes contains typeof(obj), compiler will not check obj's subtypes.

When compiler resolves binary expressions, where both arguments have a alias this declarations, compiler proceeds as follows: At the first stage compiler tries to resolve alias this only for one term: binex(a, b) -> binex(a.aliasthis, b) binex(a, b) -> binex(a, b.aliasthis)

If there is only one candidate, compiler chooses it, if there are many candidates, compiler raises an error. If there isn't candidates, compiler tries to resolve both terms: binex(a, b) -> binex(a.aliasthis, b.aliasthis) If there is only one candidate, compiler chooses it. If there are several candidates, compiler raises an error.

Limitations

If type T has alias this declarations and opDispatch declarations at the same time, a compile time error will be raised. Type shouldn't have alias this and opDispatch both. This rule may be relaxed in future, but now it is the simplest way to avoid symbol hijacking between different sybtyping methods.

Now sybtyping via inheritance has a much high priority then sybtyping via alias this. Thus base (inherited) type I can hijack symbol from derived type D, if D uses both alias this and inheritance sybtyping:

    class I
    {
        
    }

    struct A
    {
        void foo()
        {
            writeln("A");
        }
    }

    class D : I
    {
        A a;
        alias a this;
    }

    void main()
    {
        (new D).foo(); //prints "A"
    }

Now if we add foo method to class I, foo will be hijacked:

    class I
    {
        void foo()
        {
            writeln("I");
        }   
    }

    struct A
    {
        void foo()
        {
            writeln("A");
        }
    }

    class D : I
    {
        A a;
        alias a this;
    }

    void main()
    {
        (new D).foo(); //prints "I"
    }

At the first look, it would be fine to disallow this case and raise an error if there are conflict between inherited and "alias this"-ed symbols. However, this change will break a lot of user code (simple alias this is present in the language for a long time) and resolving of this situation should be deferred to another DIP.