haXe

From Wikipedia, the free encyclopedia

Jump to: navigation, search
haXe
Paradigm Multi-paradigm
Appeared in 2005
Developer Nicolas Cannasse
Latest release 2.0
Typing discipline static
Influenced by ActionScript, OCaml
OS Cross platform
Website haxe.org

haXe (pronounced hex[1]) is a programming language, designed for creating interactive web applications. Currently there are four official compiler targets - Adobe Flash, JavaScript, PHP and the Neko VM.[2]

Contents

[edit] History

Development of haXe was started on the 22nd of October, 2005. The first alpha version of haXe was released on the 14th of November, in the same year. The first beta version was released on the 4th of February, 2006. haXe 1.0 was released on the 17th of April, in the same year. The current release of haXe is version 2.0, which was released on the 28th of July, 2008.[3]

haXe is released under the GNU General Public License.[4]

[edit] Naming and pronunciation

The name haXe (pronounced as hex, although the authors of the only published book on the language pronounce it "hacks"[5]) was chosen because it is short, easy, cool, and "has an X inside", which the author claims is necessary to make any new technology a success.[6]

[edit] Language

The header of the official haXe site describes haXe as a "web oriented universal language." The feature that is tied to this description is the compiler's ability to target different virtual machines: Adobe Flash's AVM1(Flash 6-8) and AVM2(Flash 9+), Javascript, and NekoVM, the last being a general-purpose virtual machine also built by haXe creator Nicolas Cannasse. The haXe language and libraries are designed to achieve maximal support of these target platforms, and the compiler will emit target source or bytecode depending on the language and options selected. The "web oriented universal" moniker follows from this ability to deploy the same code across different layers of a WWW application. The haXe community has shown interest in adding PHP[7] and Java[8] support.

haXe is similar to ECMAScript, although almost no ECMAScript code will run on haXe without modifications. Unlike ECMAScript, haXe is a compiled language. It is a general-purpose language with object-oriented programming, exceptions, and type inference with class parameters. Generic classes, reflectivity, iterators, and functional programming are built-in functionality of the language and libraries.[9] Unusual among programming languages, haXe contains a type system which is both strong and dynamic. The compiler will check types implicitly and give compile-time errors, but it also enables the programmer to bypass type-checking and rely on the target platform's dynamic type-handling.

Function parameters can be defined very precisely in haXe:

function multipleparams(threedimensionalarray : Array<Array<Array<Int>>>, stringval : String, boolval : Bool) {}
function optionalargument( ?i : Int ) : Int {return 0;} // optional int value returning an int
function functionasparameter( f : Void -> Void ) {f();} // call a function with no parameters 
function anotherfunctionparm( f : Int -> Int ) {var result = f(1);} // call a function that returns an int, with an int parameter
function icoulddoanything(d : Dynamic) : Dynamic {return d;} // function which takes any kind of type and returns it

Enumerated types are a key feature of the language, they can have parameters of their own and be recursive, which allows them to be treated like classes.[10] Enums in haXe are not simply indexed "magic-number" values as in most languages, but are more abstract: they contain no inherent value, but can be instanced into variables as in this example:

    enum Color {
        red;
        green;
        blue;
        rgb: ( r : Int, g : Int, b : Int );
    }
 
    class Colors {
        static function toInt( c : Color ) : Int {
            return switch( c ) {
                case red: 0xFF000;
                case green: 0x00FF00;
                case blue: 0x0000FF;
                case rgb(r,g,b): (r << 16) | (g << 8) | b;
            }
        }
        static function validCalls() {
             var redint = toInt(red);
             var rgbint = toInt(rgb(100,100,100));            
        }
    }

(Modified from the haXe Reference)

Demonstrating haXe for graphical uses, here is some example code for the Adobe Flash target (from the haXe website):

class Test {
    static function main {
        var mc : flash.MovieClip = flash.Lib.current;
        mc.beginFill(0xFF0000);
        mc.moveTo(50,50);
        mc.lineTo(100,50);
        mc.lineTo(100,100);
        mc.lineTo(50,100);
        mc.endFill();
    }
}

This will draw a square using a Flash MovieClip object.

[edit] Compiler implementation and performance

The haXe compiler is implemented in the OCaml language. Because haXe-generated code runs on virtual machines, no knowledge of OCaml is necessary to develop applications using haXe. This also means that benchmark performance varies depending on the target platform, as each platform must be customized to make the best use of available features.

One benchmark[11] indicates that haXe compiles Flash 9(AVM2) bytecode with better performance than the equivalent AS3 code on Adobe's compiler. Since publication of this benchmark the compiler has further improved performance features with the addition of inline functions.

[edit] References

[edit] External links

Personal tools