Home

This post is unpublished! This means that it will not show on feeds or the main page. To publish your post, edit this page, find the field that says "Publish?", click on it, click on the Publish button, then save the page.

 

alt="Not

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

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, 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>.

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(){}
}

  • 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.
  • I will discuss accessors more in detail in a future post.

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
  • Haxe uses the "in" keyword like Java uses the colon (:) operator

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

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:Map<String, Int> = new Map<String, Int>();
table["key"] = 1;

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

for (key in table.keys()) { }

table.remove("key");

  • Haxe has a new Map structure that handles hash tables more elegantly

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.

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!

  0

Add a New Comment