Unification is the heart of the type system and contributes immensely to the robustness of Haxe programs. It describes the process of checking if a type is compatible with another type.
Define: Unification
Unification between two types A and B is a directional process which answers one question: whether A can be assigned to B. It may mutate either type if it either is or has a monomorph.
Unification errors are very easy to trigger:
class Main { static public function main() { // Int should be String var s:String = 1; } }
We try to assign a value of type Int
to a variable of type String
, which causes the compiler to try and unify Int with String. This is, of course, not allowed and makes the compiler emit the error Int should be String
.
In this particular case, the unification is triggered by an assignment, a context in which the "is assignable to" definition is intuitive. It is one of several cases where unification is performed:
a
is assigned to b
, the type of a
is unified with the type of b
.return e
expression, the type of e
is unified with the function return type. If the function has no explicit return type, it is inferred to the type of e
and subsequent return
expressions are inferred against it.a && b
unifies both a
and b
with Bool
and the expression a == b
unifies a
with b
.