HaxeDoc2

(やまだぃちぅ) #1
In this example we first cast a class instance of typeChild1toBase, which succeeds because
Child1is a child class (2.3.2) ofBase. We then try to cast the same class instance toChild2,
which is not allowed because instances ofChild2are not instances ofChild1.
The Haxe compiler guarantees that an exception of typeStringis thrown (5.22) in this case.
This exception can be caught using atry/catchblock (5.18).
Safe casts have a runtime overhead. It is important to understand the the compiler already
generates type checks, so it is redundant to add manual checks, e.g. usingStd.is. The intended
usage is to try the safe cast and catch theStringexception.

5.24 type check


Since Haxe 3.1.0

It is possible to employ compile-time type checks using the following syntax:

1 (expr : type)


The parentheses are mandatory. Unlike safe casts (5.23.2) this construct has no run-time im-
pact. It has two compile-time implications:

1.Top-down inference (3.6.1) is used to typeexprwith typetype.

2.The resulting typed expression is unified (3.5) with typetype.

This has the usual effect of both operations such as the given type being used as expected
type when performing unqualified identifier resolution (3.7.3) and the unification checking for
abstract casts (2.8.1).
Free download pdf