HaxeDoc2

(やまだぃちぅ) #1
It is common for programming languages to have a single, clean definition for nullability. How-
ever, Haxe has to find a compromise in this regard due to the nature of Haxe’s target languages:
While some of them allow and; in fact, default tonullfor anything, others do not even allow
nullfor certain types. This necessitates the distinction of two types of target languages:

Definition: Static target
Static targets employ their own type system wherenullis not a valid value for basic types.
This is true for the Flash, C++, Java and C# targets.

Definition: Dynamic target
Dynamic targets are more lenient with their types and allownullvalues for basic types.
This applies to the JavaScript, PHP, Neko and Flash 6-8 targets.

There is nothing to worry about when working withnullon dynamic targets; however, static
for starters...please re- ones may require some thought. For starters, basic types are initialized to their default values.
view


for starters...please re-
view


Definition: Default values
Basic types have the following default values on static targets:

Int: 0

Float: NaNon Flash,0.0on other static targets

Bool: false

As a consequence, the Haxe Compiler does not allow the assignment ofnullto a basic type
on static targets. In order to achieve this, the basic type has to be wrapped asNull<T>:
1 // error on static platforms
2 var a:Int = null;
3 var b:Null<Int>=null; // allowed
Similarly, basic types cannot be compared tonullunless wrapped:
1 var a : Int = 0;
2 // error on static platforms
3 if( a == null ) { ... }
4 var b : Null<Int> = 0;
5 if( b != null ) { ... } // allowed
This restriction extends to all situations where unification (3.5) is performed.

Type:Null<T>
On static targets the typesNull<Int>,Null<Float>andNull<Bool>can be used to
allownullas a value. On dynamic targets this has no effect.Null<T>can also be used with
other types in order to document thatnullis an allowed value.

If anull-value is “hidden” inNull<T>orDynamicand assigned to a basic type, the default
value is used:
Free download pdf