3.7.3 Resolution Order
Resolution order comes into play as soon as unqualified identifiers are involved. These are expressions in the form of foo()
, foo = 1
and foo.field
. The last one in particular includes module paths such as haxe.ds.StringMap
, where haxe
is an unqualified identifier.
We describe the resolution order algorithm here, which depends on the following state:
- the declared local variables (including function arguments)
- the imported modules, types and statics
- the available static extensions
- the kind (static or member) of the current field
- the declared member fields on the current class and its parent classes
- the declared static fields on the current class
- the expected type
- the expression being
untyped
or not
Figure: Resolution order of identifier `i'
Given an identifier i
, the algorithm is as follows:
- If i is
true
,false
,this
,super
ornull
, resolve to the matching constant and halt. - If a local variable named
i
is accessible, resolve to it and halt. - If the current field is static, go to 6.
- If the current class or any of its parent classes has a field named
i
, resolve to it and halt. - If a static extension with a first argument of the type of the current class is available, resolve to it and halt.
- If the current class has a static field named
i
, resolve to it and halt. - If an enum constructor named
i
is declared on an imported enum, resolve to it and halt. - If a static named
i
is explicitly imported, resolve to it and halt. - If
i
starts with a lower-case character, go to 11. - If a type named
i
is available, resolve to it and halt. - If the expression is not in untyped mode, go to 14
- If
i
equals__this__
, resolve to thethis
constant and halt. - Generate a local variable named
i
, resolve to it and halt. - Fail
For step 10, it is also necessary to define the resolution order of types:
- If a type named
i
is imported (directly or as part of a module), resolve to it and halt. - If the current package contains a module named
i
with a type namedi
, resolve to it and halt. - If a type named
i
is available at top-level, resolve to it and halt. - Fail
For step 1 of this algorithm as well as steps 5 and 7 of the previous one, the order of import resolution is important:
- Imported modules and static extensions are checked from bottom to top with the first match being picked.
- Within a given module, types are checked from top to bottom.
- For imports, a match is made if the name equals.
- For static extensions, a match is made if the name equals and the first argument unifies. Within a given type being used as static extension, the fields are checked from top to bottom.