Cheat Sheet
It is easy to transition to Haxe from ActionScript. In fact, the Haxe programming language was created by Flash developers. It started with MTASC, the open-source ActionScript 2 compiler, and evolved to the Haxe we love and know today. Haxe supports many advanced features ActionScript wishes it supported.
This is a “cheat sheet” with a list of areas where Haxe and Actionscript 3 differ from one another. Places where the languages are the same have not been included. The column on the left is AS3, and the column on the right is Haxe.
Basic Types
Boolean int Number Object void Array Vector.<String>
Bool Int Float Dynamic Void Array Array
Package Declarations
package com.example.myapplication { }
package com.example.myapplication;
Defining a Class
public class MyClass { public function MyClass () { } }
class MyClass { public function new () { } }
Loops
for (var i:uint = 0; i < 100; i++) { } for each (var value:String in items) { } for (var propertyName:String in object) { }
for (i in 0...100) { } for (value in items) { } var fields = Reflect.fields (object); for (propertyName in fields) { }
Switch Statements
switch (value) { case 1: trace ("Equal to 1"); break; default: trace ("Not equal to 1"); break; }
switch (value) { case 1: trace ("Equal to 1"); default: trace ("Not equal to 1"); }
Type Inference
var hi = "Hello World"; // type is Object // fails to compile in strict mode
var hi = "Hello World"; // type is String // even works for code completion
Type Casting
var car:Car = vehicle as Car; var toString:String = String (10); var toNumber:Number = Number ("10"); var toInteger:int = int (10.1);
var car:Car = cast vehicle; // or for a safe cast: var car = cast (vehicle, Car); var toString = Std.string (10); var toNumber = Std.parseFloat ("10"); var toInteger = Std.int (10.1);
Type Details
if (vehicle is Car) { } import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; name = getQualifiedClassName (vehicle); type = Class (getDefinitionByName (name);
if (Std.is (vehicle, Car)) { } type = Type.getClass (vehicle); name = Type.getClassName (type);
Checking for Null
if (object == null) { } if (!object) { }
if (object == null) { }
Hash Tables
var table:Object = new Object (); table["key"] = 100; trace (table.hasOwnProperty ("key")); for (var key:Object in table) { trace (key + " = " + table[key]); } delete table["key"];
var table = new Map<String,Int> (); table.set ("key", 100); trace (table.exists ("key")); for (key in table.keys ()) { trace (key + " = " + table.get (key)); } table.remove ("key");
Rest Parameters
function test (...params):void { } test (1, 2, 3);
function test (params:Array) { } Reflect.makeVarArgs (test) (1, 2, 3);
Reflection
var foo = object["foo"]; bar.apply (this, [ "hi" ]);
var foo = Reflect.field (object, "foo"); Reflect.callMethod (this, bar, [ "hi" ]);
Constants
private const gravity:Number = 9.8;
inline private static var gravity = 9.8;
Function Types
function hello (msg:String):void { } var type:Function = hello;
function hello (msg:String):Void { } var type:String->Void = hello; // can also use Dynamic
Getters and Setters
function get x ():Number { return _x; } function set x (value:Number):void { _x = value; }
public var x (get, set):Float; function get_x ():Float { return _x; } function set_x (value:Float):Float { return _x = value; }
Read-Only Properties
function get x ():Float { return _x; }
public var x (default, null):Float; // null allows private access // `never` would restrict all access
Missing Features
Haxe does not currently support custom namespaces, and methods do not provide an arguments property.
Additional Features
In addition to most of the features of Actionscript 3, Haxe includes support for enums, type parameters (generics), structures, typedefs, custom iterators, conditional compilation, inlining and more.
Additional Packages
Haxe comes with a standard set of classes which are specific to each platform. If you are targeting Flash, you can use regular classes from the flash.* package. If you are targeting Javascript, you can use DOM and browser specific code using the js.* package.
To read files, directories or perform other common system functions, you can access classes in the cpp.* or neko.* packages, depending on whether you use C++ or Neko when compiling for the desktop.
OpenFL provides a consistent subset of the flash.* package, for C++ platforms, Neko and HTML5. OpenFL has some additional features, which will usually be under the openfl.* package.
Miscellaneous Notes
In Haxe, class packages must be lower-case, and class names must begin with a capital letter. This convention is looser in Actionscript 3. You must also import classes by name, so don’t use wildcards.
The private namespace in Haxe is similar to the protected namespace in Actionscript 3.