Syntax
Case Sensitivity
ActionScript 3.0 and Haxe are both case-sensitive languages, so both of the following are different variables:
var hello:String;
var Hello:String;
Semicolons
ActionScript 3.0 and Haxe both use semicolons (;) to terminate a statement. Good coding standards in ActionScript encourage the use of semicolons, but they are optional.
In Haxe, the use of semicolons is not optional.
var hello:String; // correct
var hello:String // incorrect
Parentheses
In ActionScript 3.0, parentheses (()) can be used in three ways, but only two of these are valid in Haxe.
You can use parentheses to group operations:
trace (1 + (2 * 3)); // 7
trace ((1 + 2) * 3); // 9
You can also use parentheses to pass one or more parameters to functions or methods:
trace ("hello");
ActionScript also supports the use of commas to separate multiple expressions and return the result of the final expression. For example:
var a:int = 1;
var b:int = 2;
trace ((a++, b++, a + b)); // 5
The above syntax can be confusing, and is not supported in Haxe. The same code can be handled by expanding the expression:
var a = 1;
var b = 2;
a++;
b++;
trace (a + b);
...or using a function:
var a = 1;
var b = 2;
trace (function () { a++; b++; return a + b; } ());
Code Blocks
Code encased by curly braces ({}) are called a code block. The body of many portions of ActionScript and Haxe are grouped together using code blocks.
public function hello ():String {
return "World";
}
if (raining) {
useUmbrella ();
}
Whitespace
Spaces, tabs and line breaks are referred to as whitespace. Actionscript and Haxe both allow whitespace to make code easier to read.
class Test {
public function new () {
}
}
class Test2 { public function new() {} }
Comments
Actionscript and Haxe both allow for single-line or multi-line comments. A comment will not be compiled, allowing the addition of helpful notes or as a method of quickly disabling code.
// This is a single line comment
/* This is
a multi-line
comment */
Literals
A literal is a fixed value in your code, Haxe and ActionScript are similar in the types of literals you can use:
100
-2.1
"hello"
null
true
false
ActionScript has an undefined literal which is not used in Haxe. The following returns undefined in ActionScript, but is a compile error in Haxe:
object = { a: "b", c: "d" };
trace (object.e);
Instead, use the Haxe Reflect class to check if a field exists, or use static types.
if (Reflect.hasField (object, "e")) {
trace (Reflect.field (object, "e"));
}
Square Brackets
In ActionScript and Haxe, square brackets ([]) can be used to define an array literal:
var colors = [ "red", "blue", "green" ];
Square brackets can also be used to access array values:
var firstColor = colors[0];
Haxe allows support for array access on user-created abstract objects, in addition to core types.
ActionScript also allows the use of square brackets to access fields using a string-based name, but this is not supported in Haxe. Use the Reflect class in Haxe in order to reference a field dynamically.
ActionScript 3.0
clip.x = 100;
clip["y"] = 100;
Haxe
clip.x = 100;
Reflect.setField (clip, "y", 100);
Angle Brackets
ActionScript 3.0 uses angle brackets (<>) for the Vector.<>. Haxe provides support for type parameters in the Array<>, Map<> and multiple other types in the language, and allows use of type parameters in user classes as well.
When describing the type of an Array, ActionScript will always treat the array as if it is dynamic. In Haxe, arrays are typed based on their contents, which improves performance and can eliminate confusing errors:
var values:Array<Int> = [ 0, 1, 2, 5 ];
var names:Array<String> = [ "Doug", "Richard", "Harrison" ];
Arrow Tokens
Haxe supports a special arrow token (->) syntax, which is used when describing a function. The ActionScript does not allow for different types based on the signature of a function. Instead, every function in ActionScript is described as type Function. Haxe allows for Dynamic to describe any function, but uses "arrow tokens" when a function signature is described specifically. For example, a function that accepts a Bool and returns an Int would be described as Bool->Int, and a function that accepts two boolean arguments instead would be Bool->Bool->Int
private function onMouseDown (x:Float, y:Float):Void {
}
var handler:Float->Float->Void = onMouseDown;
handler (100, 100);
Keywords
ActionScript and Haxe share many reserved words and keywords:
break |
case |
catch |
class |
continue |
default |
do |
dynamic |
else |
extends |
false |
for |
function |
if |
implements |
import |
in |
interface |
new |
null |
override |
package |
private |
public |
return |
static |
super |
switch |
this |
throw |
true |
try |
var |
while |
The Haxe programming language is designed to limit the number of keywords, so there are a number of keywords which are in ActionScript 3.0 that are not keywords in Haxe:
asIn ActionScript 3.0,
asis used for casting. Adobe referencescastas a "future" keyword that has not been implemented in the language.var clip:MovieClip = event.currentTarget as MovieClip;Haxe uses
castwhen casting between types, in order to perform a safe cast (which is slower, but throws an error if it is not possible), use parentheses:var clip = cast (event.currentTarget, MovieClip);...otherwise, an unsafe cast occurs without parentheses and a second argument.
var clip:MovieClip = cast event.currentTarget;constActionscript uses the
constkeyword to define a constant value. Because Haxe supports inlining, aconstkeyword is not necessary.public static inline var gravity = 9.8;eachActionScript has separate "for in" or "for each in" loops, but in Haxe these are simplified to a single "for in" loop.
var fruits = [ "apple", "pear", "orange" ]; for (fruit in fruits) { trace (value); }If you need to iterate over the names of the fields in an object, use
Reflect.fields:```haxe var object = { a: "b", c: "d" };
for (field in Reflect.fields (object)) {
trace (field); // returns "a" then "c"} ```haxe
finalActionScript uses the
finalkeyword to specify a class that cannot be extended. Haxe supports the same, but it uses a@:finalmeta-data tag instead:@:final class Test { }finallyHaxe does not support
try/catch/finallyso cleanup logic must occur inside acatchin addition to after thetry/catchstatement.includeHaxe does not support an
includekeyword, there are methods of replicating the same behavior using a custom macro, but in general, it is recommended that classes, typedefs,usingand other techniques are implemented to share code within a project.instanceofThe ActionScript 3.0 keyword,
instanceof, can be replaced withStd.istrace (Std.is (sprite, DisplayObject)); trace (Std.is ("hello", String)); trace (Std.is (5, Int));internalHaxe has support for both
publicandprivatekeywords, which are simpler to understand, and cover the majority of use cases.Haxe supports three meta-data tags (
@:allow,@:accessand@:privateAccess) which each provide different methods of accessing aprivatemember. This can provide the same function asinternal, or like "friend classes" in other languages.class Secret { private var password:String; }@:access(Secret) class Login { public function new () { var password = Secret.password; } }namespaceHaxe does not support custom namespaces like ActionScript 3.0.
nativeActionScript has a
nativekeyword when a method is a wrapper around a native method. Haxe hasexternclasses for working with native APIs, and depending on the platform, "CFFI" or "JNI" for accessing methods or properties between different runtime environments.On HTML5, it is possible to also implement
untypedJavaScript code directly.untyped __js__ ("window.alert ('Hello!')");...extern classes tend to help provide a nicer API, however:
Browser.alert ("Hello!");protectedThe
privatekeyword in Haxe behaves similar to theprotectedkeyword in ActionScript.typeofActionScript 3.0 uses the
typeofkeyword to return a generic String value for the current object:switch (typeof obj) { case "boolean": trace ("obj is a boolean"); break; case "number": trace ("obj is a number"); break; default: }Haxe allows a similar check using
Type.typeof, though it returns a static type (not a String).switch (Type.typeof (obj)) { case TBool: trace ("obj is a boolean"); case TInt, TFloat: trace ("obj is a number"); default: }useHaxe does not support custom namespaces like ActionScript 3.0.
voidHaxe uses
Voidinstead ofvoid(for better consistency with other type names).withThe ActionScript 3.0
withkeyword is a shorthand approach that can reduce the amount of code that is written, but it has some limitations (such as not being able to declare new variables inside) which can make it unpredictable to use.var clip = new MovieClip (); with (clip) { x = 100; y = 100; }Haxe does not support the
withkeyword, it may make code more verbose, but it can make code simpler to understand and more reliable.var clip = new MovieClip (); clip.x = 100; clip.y = 100;
Haxe also has additional keywords that are not defined in ActionScript:
cast |
enum |
extern |
inline |
never |
typedef |
untyped |
using |