HaxeDoc2

(やまだぃちぅ) #1

1 class MathStaticExtension {
2 / Converts an angle in radiansto degrees /
3 inline public static function
4 toDegrees (radians :Float):Float
5 {
6 return radians * 180/Math.PI;
7 }
8 }


1 using MathStaticExtension;
2 class TestMath{
3 public static function main(){
4 var ang = 1/2*Math.PI;
5 trace(ang.toDegrees()); // 90
6 }
7 }


10.5 Lambda............................................


10.6 Reflection...........................................


Haxe supports runtime reflection of types and fields. Special care has to be taken here because
runtime representation generally varies between targets. In order to use reflection correctly it
is necessary to understand what kind of operations are supported and what is not. Given the
dynamic nature of reflection, this can not always be determined at compile-time.
The reflection API consists of two classes:

Reflect: A lightweight API which work best on anonymous structures (2.5), with limited support
for classes (2.3).

Type:A more robust API for working with classes and enums (2.4).

The available methods are detailed in the API forReflectandType.
Reflection can be a powerful tool, but it is important to understand why it can also cause
problems. As an example, several functions expect a String (10.1) argument and try to resolve it
to a type or field. This is vulnerable to typing errors:

1 class Main {
2 static function main() {
3 trace(Type.resolveClass("Mian")); // null
4 }
5 }


However, even if there are no typing errors it is easy to come across unexpected behavior:

1 class Main {
2 static function main() {
3 // null
4 trace(Type.resolveClass("haxe.Template"));
5 }
6 }

Free download pdf