HaxeDoc2

(やまだぃちぅ) #1

2.5.2 Class Notation for Structure Types........................


When defining a structure type, Haxe allows using the same syntax as described inClass Fields
(Chapter 4). The following typedef (3.1) declares aPointtype with variable fieldsxandyof
typeInt:
1 typedef Point ={
2 var x : Int;
3 var y : Int;
4 }

2.5.3 Optional Fields


I don’t really know
how these work yet.


I don’t really know
how these work yet.


2.5.4 Impact on Performance


Using structures and, by extension,structural subtyping (3.5.2) has no impact on performance
when compiling to dynamic targets (2.2). However, on static targets (2.2) a dynamic lookup has
to be performed which is typically slower than a static field access.

2.6 Function Type


It seems a bit convo-
luted explanations.
Should we maybe
start by ”decoding”
the meaning of Void -¿
Void, then Int -¿ Bool
-¿ Float, then maybe
have samples using
$type


It seems a bit convo-
luted explanations.
Should we maybe
start by ”decoding”
the meaning of Void -¿
Void, then Int -¿ Bool
-¿ Float, then maybe
have samples using
$type


The function type, along with the monomorph (2.9), is a type which is usually well-hidden
from Haxe users, yet present everywhere. We can make it surface by using$type, a special Haxe
identifier which outputs the type its expression has during compilation :
1 class FunctionType {
2 static public function main() {
3 // i : Int -> s : String -> Bool
4 $type(test);
5 $type(test(1, "foo")); // Bool
6 }
7
8 static function test(i:Int, s:String):Bool {
9 return true;
10 }
11 }
There is a strong resemblance between the declaration of functiontestand the output of the
first$typeexpression, yet also a subtle difference:


  • Function argumentsare separated by the special arrow token->instead of commas, and

  • thefunction return typeappears at the end after another->.


In either notation it is obvious that the functiontestaccepts a first argument of typeInt,
a second argument of typeStringand returns a value of typeBool. If a call to this function,
such astest(1, "foo"), is made within the second$typeexpression, the Haxe typer checks
if 1 can be assigned toIntand if"foo"can be assigned toString. The type of the call is then
equal to the type of the valuetestreturns, which isBool.
Free download pdf