Haxe shares many similarities with the Actionscript 3 (AS3) language, and therefore AS3 developers like myself can pick it up rather easily. There are, of course, some immediate differences that should be noted if wanting to write Haxe code, and I attempt to note the most important ones here. This post borrows and augments the information found at OpenFL's site.
Please note that this is by no means a complete guide to Haxe! One of the most glaring differences between AS3 and Haxe is Haxe's incredibly expansive functionality. For instance, Haxe allows for generic type parameters whereas AS3 does not; such a feature can be found in Java generics and C++ templates. Haxe furthermore allows for Enum structures, inlining, type abstraction, and much more. I will explore such things in later posts, but you can get a head start by peeking at Haxe's manual.
Below, though, you can find some quick conversions from AS3 code to Haxe, along with some notes about how Haxe compares with Java or C++ for those more familiar with those languages.
Basic Types
Permalink to "Basic Types"- 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, I believe that Dynamic is more similar to the * (untyped) in AS3. Dynamic removes compile-time type checking just like the * typing in AS3.
- We essentially replace Vectors with Arrays. In Java, this is the same as ArrayList<T>, and in C++, we use vector<T>.
- int
- uint
- Number
- Boolean
- void
- Object
- Array
- Vector.<T>
Haxe
Permalink to "Haxe"- Int
- UInt
- Float
- Bool
- Void
- Dynamic (?)
- Array<Dynamic>
- Array<T>
Class Definition
Permalink to "Class Definition"- Constructors in Haxe are now named
new
rather than the class name - Do NOT include
public
beforeclass
in Haxe
package com.newprogrammer {
public class Circle extends Shape implements Drawable {
public function Circle() { }
}
}
Haxe
Permalink to "Haxe"package com.newprogrammer;
class Circle extends Shape implements Drawable {
public function new() { }
}
Properties
Permalink to "Properties"- There is no
const
keyword in Haxe. Instead we useinline var
for constants. - I will discuss accessors more in detail in a future post.
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
Permalink to "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;
}
}
For Loops
Permalink to "For Loops"- for loops do not support the traditional C syntax like AS3 or Java do
- for loops only work on iterable entities
- Haxe uses the "
in
" keyword like Java uses the colon (:) operator
for(var i: int = 0; i < 100; ++i) { }
for each(var v: T in items) { }
Haxe
Permalink to "Haxe"for(i in 0...100) { }
for(v in items) { }
Switch Statements
Permalink to "Switch Statements"- 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
switch(value) {
case 1:
trace("Value is 1");
break;
case 2:
trace("Value is 2");
break;
default:
trace("Default reached");
}
Haxe
Permalink to "Haxe"switch(value) {
case 1:
trace("Value is 1");
case 2:
trace("Value is 2");
default:
trace("Default reached");
}
Hash Tables
Permalink to "Hash Tables"- Haxe has a new Map structure that handles hash tables more elegantly
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
Permalink to "Haxe"var table = new Map<String, Int>();
table["key"] = 1;
if (table.exists("key"))
trace(table["key"]);
for (key in table.keys()) { }
table.remove("key");
Other Differences
Permalink to "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 toprotected
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.
OpenFL's page specifies even more differences regarding casting and reflection. In future posts, I will begin looking at some of Haxe's features in more depth.
And, since it is 25 December, Merry Christmas to all readers!