Difference between revisions of "DIP66"
(→Semantics: Make lookup an ordered list.) |
IgorStepanov (talk | contribs) (→Description) |
||
Line 42: | Line 42: | ||
... 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>. | ||
This rule applies to implicit and explicit conversion, <code>.member</code> access expression, operator overloading, foreach expression (<code>foreach(args; ''obj'')</code>) etc. | 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 a | + | <code>''symbol''</code> can be an any symbol when obj.symbol is a valid expression. |
If more than one <code>alias this</code> can be used to solve the same lookup, the compiler should raise an error. | If more than one <code>alias this</code> can be used to solve the same lookup, the compiler should raise an error. | ||
Revision as of 21:51, 29 October 2014
Title: | (Multiple) alias this |
---|---|
DIP: | 66 |
Version: | 1 |
Status: | Draft |
Created: | 2014-10-09 |
Last Modified: | 2014-10-19 |
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
.
This rule applies to implicit and explicit conversion, .member
access expression, operator overloading, foreach expression (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, the compiler should raise an error.
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)); //Ok, because C is subtype of int anyway.
Semantics
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:
- If
xyz
is a symbol (member, method,enum
etc) defined insidetypeof(obj)
then lookup is done. - Otherwise, if
xyz
is a symbol introduced in the base class (where applicable), then lookup is done. - Otherwise, if
opDispatch!"xyz"
exists, then lookup is done. - Otherwise,
alias this
is attempted transitively, and ifxyz
is found, then lookup is done. - 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 a result set. If application fails, the compiler tries to recursively resolve the alias this
expression. The following pseudo-code illustrates this:
resolveAliasThis(obj, ex):
Set resultSet;
foreach currentAliasThis in obj.aliasThisSymbols do
if try(`ex(obj.currentAliasThis))` == Success then
resultSet.add(`ex(obj.currentAliasThis)`)
else
resultSet.add(resolveAliasThis(`obj.currentAliasThis`, ex))
if obj is class then
foreach currentBaseClass in obj.baseClasses do
resultSet.add(resolveAliasThis(`cast(currentBaseClass)obj`, ex))
return resultSet
Finally, if resultSet contains only one candidate, the compiler will accept it. If resultSet is empty, compiler tries another ways to resolve ex(obj): UFCS et c. 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.