EP  :

                   

This page suggests that you have knowledge about the following subjects:

  • Knowledge of the AS3 language and syntax

Haxe has many syntactic similarities with the Actionscript 3 (AS3) language. If you have significant experience with AS3 Flash development, then picking up Haxe should be exceptionally simple, especially when using OpenFL. That said, there are in fact some immediate differences that ought to be noted, the most important of which are listed here.

This article serves as a direct syntax conversion from AS3 code to Haxe code, borrowing and augmenting the information found at OpenFL's site. To learn more about Haxe's features, see Haxe in a Nutshell and Haxe vs Other Languages.

Basic Types

AS3

  1. int
  2. uint
  3. Number
  4. Boolean
  5. void
  6. Object
  7. Array
  8. Vector.<T>

Haxe

  1. Int
  2. UInt
  3. Float
  4. Bool
  5. Void
  6. Dynamic (?)
  7. Array<Dynamic>
  8. Array<T>
  • Both Number and Float are IEEE double-precision numbers and use 64 bits
  • Many sources will compare Haxe's Dynamic construct to AS3 or Java's Object class. However, Dynamic is more similar to the * (untyped) in AS3. Dynamic removes compile-time type checking just like the * typing in AS3.
  • Arrays replace Vectors. In Java, this is the same as ArrayList<T>, and in C++, it is vector<T>.

Class Definition

AS3

package com.newprogrammer{
    public class Circle extends Shape implements Drawable{
        public function Circle(){}
    }
}

Haxe

package com.newprogrammer;

class Circle extends Shape implements Drawable{
    public function new(){}
}

  • Packages in Haxe do not begin blocks as they do in AS3
  • Constructors in Haxe are now named new rather than the class name
  • Do NOT include public before class in Haxe

Properties

AS3

public class Circle{
    public static const PI:Number = 3.1415;

    private var _radius:Number;

    public function get radius():Number{
        return _radius;
    }
    public function set radius(value:Number):void{
        if(value < 0)
            throw "Error";
        _radius = value;
    }
}

Haxe

class Circle{
    public static inline var PI:Float = 3.1415;
    public var radius(default, set):Float;

    public function set_radius(value:Float):Float{
        if(value < 0)
            throw "Error";
        return radius = value;
    }
}

  • There is no const keyword in Haxe. Instead we use inline var for constants.
  • Accessors are described in further detail on the Accessors page.

For Loops

AS3

for(var i:int = 0; i < 100; ++i){}

for each(var v:T in items){}

Haxe

for(i in 0...100){}

for(v in items){}

  • for loops do not support the traditional C syntax like AS3 or Java do
  • for loops only work on iterable entities

Switch Statements

AS3

switch(value){
    case 1:
        trace("Value is 1");
        break;
    case 2:
        trace("Value is 2");
        break;
    default:
        trace("Default reached");
}

Haxe

switch(value){
    case 1:
        trace("Value is 1");
    case 2:
        trace("Value is 2");
    default:
        trace("Default reached");
}

  • Haxe always has an implicit break statement for each case
  • Haxe allows the use of pattern matching in case expressions (not pictured above), making switch statements overall very powerful

Type Inference

In either language, variables can be declared without explicit typing. For instance, the following will work:

var s = "A String";

However, how AS3 and Haxe interprets this statement is different. If you check the type of the variable s, in AS3 it will be Object, but in Haxe it will be String.

This is because Haxe employs a powerful type inference mechanism that allows it to determine the type of an object purely from how the object is used. This even works for code completion, and hence it is generally recommended to use Haxe's type inference to simplify code.

However, a couple of limitations exist:

  • Field instances should always be explicitly typed unless initialized
  • Haxe never infers anything as Dynamic, and hence all Dynamic objects must be explicitly declared as such

Casting

AS3

var c:Child = base as Child;
var c:Child = Child(base);
 
var toString:String = String(7);
var toNumber:Number = Number("7");

Haxe

var c:Child = cast base;   // unsafe
var c = case(base, Child); // safe
 
var toString = Std.string(7);
var toNumber = Std.parseFloat("7");

  • In AS3, the as operator will cast the given object into the given type if possible, returning null upon failure. The second method will instead throw an error upon failure.
  • In Haxe, this is comparable to, though not precisely the same as, unsafe and safe casting respectively. The first will not throw any errors if the cast is inappropriate, but the second will.

Functions

AS3

function floor(n:Number):Int{}
 
var f:Function = floor;
 
function sum(...args):void{
    var s:Number = 0;
    for(var i:Int = 0; i < args.length; ++i)
        s += args[i];
    return s;
}
sum(1,5,3,7);

Haxe

function floor(n:Float):Int{}
var f:Float->Int = floor;
 
function sum(args:Array<Dynamic>):Float{
    var s = 0.0;
    for(n in args)
        s += n;
    return s;
}
sum([1,5,3,7]);
// OR
Reflect.makeVarArgs(sum)(1,5,3,7);

  • Haxe provides more detailed typing for function types
  • Haxe has a Function abstract available in the haxe package, but it behaves essentially like Dynamic
  • In the case of multiple arguments, Haxe will use a curried notation that may be familiar to Haskell programmers (eg. Float->Float->Int for a function that takes two Floats and returns an Int)

Hash Tables

AS3

var table:Object = new Object();
table["key"] = 1;

if(table.hasOwnProperty("key"))
    trace(table["key"]);

for(var key:Object in table){}

delete table["key"];

Haxe

var table = new Map<String, Int>();
table.set("key", 1);
 
if (table.exists("key"))
trace(table.get("key"));
 
for (key in table.keys()) { }
 
table.remove("key");

  • Haxe has a new Map structure that handles hash tables more elegantly
  • You may still use the array access syntax as was used in AS3 in order to get or set a value

Other Differences

Some other notable differences include:

  • Haxe allows for type inference. In AS3, not giving a variable a type will cause it to be untyped. Haxe will actually guess what the type of a value is if not given.
  • Rather than having a generic Function class, Haxe has its own way of assigning types to functions. You can read about it here.
  • Haxe's private access specifier is actually equivalent to protected in other languages, meaning that child classes can access private members of their parents. Because of this, there is no notion of a truly private variable.