HaxeDoc2

(やまだぃちぅ) #1
Here we subvert the type checker by using a cast (5.23), thus allowing the assignment in line


  1. With that we hold a referencebasesto the original array, typed asArray. This
    allows pushing another type compatible withBase(OtherChild) onto that array. However,
    our original referencechildrenis still of typeArrayand things go bad when we
    encounter theOtherChildinstance in one of its elements while iterating.
    IfArrayhad nopush()method and no other means of modification, the assignment would
    be safe because no incompatible type could be added to it. In Haxe, we can achieve this by
    restricting the type accordingly using structural subtyping (3.5.2):
    1 class Base {
    2 public function new() { }
    3 }
    4
    5 class Child extends Base {}
    6
    7 typedef MyArray={
    8 public function pop():T;
    9 }
    10
    11 class Main {
    12 public static function main () {
    13 var a =[new Child()];
    14 var b:MyArray=a;
    15 }
    16 }
    We can safely assign withbbeing typed asMyArrayandMyArrayonly having a
    pop()method. There is no method defined onMyArraywhich could be used to add incompat-
    ible types, it is thus said to becovariant.


Definition: Covariance
A compound type ( 2 ) is considered covariant if its component types can be assigned to less
specific components, i.e. if they are only read, but never written.

Definition: Contravariance
A compound type ( 2 ) is considered contravariant if its component types can be assigned to
less generic components, i.e. if they are only written, but never read.

3.5 Unification..........................................


Mention
toString()/String con-
version somewhere in
this chapter.


Mention
toString()/String con-
version somewhere in
this chapter.


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 to another type.

Definition: Unification
Unification between two types A and B is a directional process which answers the question
if Acan be assigned toB. It maymutateeither type if it is or has a monomorph (2.9).

Unification errors are very easy to trigger:
Free download pdf