Jump to content

haXe

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Xan2 (talk | contribs) at 19:00, 1 March 2012 (license). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

haXe
ParadigmMulti-paradigm
DeveloperNicolas Cannasse
First appeared2005
Stable release
2.08 / 28 September 2011; 12 years ago (28 September 2011)
Typing disciplineStatic
OSCross-platform
LicenseBSD and GPL v2
Websitehaxe.org
Influenced by
ActionScript, OCaml

haXe (pronounced hex[1] in English: /heks/[2][3]) is a versatile open-source high-level multiplatform programming language described on its website as a "universal language".[4]

It can produce:

  • Flash applications and games
  • Multi-platform client-side web applications
  • Apache CGI web applications
  • Multi-platform stand-alone applications (Windows, Linux, Mac OS), including mobile applications for platforms such as webOS or the iPhone. The latter generally requires additional SDKs to utilize the platform.

The code can be produced entirely within the haXe development environment, sometimes requiring Neko, by using a single and unmodified code-base when compiling from one target to the next.[5]

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.08, which was released on 28 September 2011.[6]

According to MTASC.org [1] HaXe is the "successor" to MTASC (the MTASC website has advertised on its homepage "MTASC successor's haXe ! See haXe.org website", and MTASC development ceased as soon as HaXe went public, and both compilers were created by Nicolas Cannasse). HaXe code therefore has much in common with AS3 but with many improvements and output capabilities which puts it in an entirely different category than the Adobe Compilers.

Originally its main advantage was the combination of wide platform-independence and a fully developed high-level language. An increasingly noteworthy advantage is its ability to use such a language to target a device-manufacturer who allows only C++ based code, their own IDE, and very little else.[1]

The haXe compiler is released under the GNU General Public License version 2.[7]

Naming and pronunciation

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

Output targets

The haXe technology derives its "universal language" moniker because of its ability to target different virtual machines supported on a plethora of OS's and devices, as well as the ability to translate the code from a haXe program into different source-code languages. This strategy of "compiling" to multiple source code languages greatly extends its ability to realize the write-once, deploy-everywhere paradigm when compared to compilers that compile directly to different operating systems. It also allows the programmer to choose the best compiler when compiling an executable, use haXe as a high-level way to quickly create complex components for a lower-level language like C++, as well as further edit and utilize libraries from another IDE.

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.

Current targets:

Targets in development:

Using these targets, one can do all the coding for any of the following entirely from within the haXe/Neko development environment requiring little if any modifications going from one target to the next:

  1. a client-side web application (by outputting to Flash, or Javascript targets),
  2. a server-side web application (by outputting to PHP, or Neko targets),
  3. a stand-alone application for Windows, Linux, or Mac OS (by outputting to C++, or Neko targets), and even ...
  4. a native-code application for mobile OS platforms like iOS,[5] Android[12] or webOS[13] by targeting output to C++ - although this last option generally requires additional SDK's to utilize the platform.

Language

Since haXe had its origins in AS3, all of the existing Flash API can be used, although haXe requires better-formed code and programming standards than do Adobe compilers (for example, with regard to scoping and data typing).

HaXe is, like AS3.0, 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.[14] 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) {
}
// optional int value returning an int
function optionalargument ( ?i : Int ) : Int {
    return 0;
}
// call a function with no parameters 
function functionasparameter ( f : Void -> Void ) {
    f();
}
// call a function that returns an int, with an int parameter
function anotherfunctionparm ( f : Int -> Int ) {
    var result = f(1);
}
// function which takes any kind of type and returns it
function icoulddoanything (d : Dynamic) : Dynamic {
    return d;
}

Enumerated types are a key feature of the language; they can have parameters of their own and be recursive.[15] They are similar to algebraic data types in languages like ML or Haskell. 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: 0xFF0000;
                case green: 0x00FF00;
                case blue: 0x0000FF;
                case rgb(r, g, b): (r << 16) | (g << 8) | b;
            }
        }
        static function validCalls() {
             var redint = toInt(Color.red);
             var rgbint = toInt(Color.rgb(100,100,100));            
        }
    }

(Modified from the haXe Reference)

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

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

This will draw a square using a Flash MovieClip object.

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[16] 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 and hidden Alchemy opcodes that Flash does not use.[17]

References

  1. ^ a b "haXe introduction page".
  2. ^ "Nicholas' answer for haXe pronunciation".
  3. ^ "The talking about haXe pronunciation in haXe official mail list".
  4. ^ http://www.haxe.org
  5. ^ a b http://gamehaxe.com/2009/07/28/haxe-iphone-cpp-at-last/
  6. ^ "News about haXe".
  7. ^ "haXe license page".
  8. ^ Professional haXe and Neko ISBN 978-0470122136
  9. ^ "haXe mailing list post on naming".
  10. ^ "haXe wiki on the community meeting of 28th of January 2010".
  11. ^ "The awesome news: new C# & Java targets for haXe. C# is coming out first, hopefully on January. Java will follow on".
  12. ^ "Blog post mentioning Android port progress".
  13. ^ "How to get started with haXe 2.06 and the webOS PDK".
  14. ^ "haXe language reference".
  15. ^ "haXe reference detailing the use of enum".
  16. ^ "AS3->haXe port of the Actionscript Physics Library".
  17. ^ http://haxe.org/doc/why

See also

  • Dart - similar programming language for web apps
  • Opa - another similar programming language for web apps
  • Monkey - another similar programming for desktop, web and mobile games.

External links