Haxe

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Haxe
Haxe logo.svg
Paradigm Multi-paradigm
Developer Haxe Foundation
First appeared 2005; 13 years ago (2005)
Stable release
3.4.7[1] / February 22, 2018; 5 months ago (2018-02-22)
Typing discipline Static, dynamic via annotations, nominal
Implementation language OCaml
Platform ARM; IA-32, x86-64
OS Android, iOS; Linux, macOS, Windows
License GPL 2.0, library: MIT
Filename extensions .hx .hxml
Website haxe.org
Influenced by
ActionScript, OCaml, Java

Haxe is a high-level cross-platform multi-paradigm programming language and compiler that can produce applications and source code, for many different computing platforms, from one code-base.[2][3][4][5] It is free and open-source software, distributed under the GNU General Public License (GPL) version 2, and the standard library under the MIT License.

Haxe includes a set of common functions that are supported across all platforms, such as numeric data types, text, arrays, binary and some common file formats.[3][6] Haxe also includes platform-specific application programming interface (API) for Adobe Flash, C++, PHP and other languages.[3][7] OpenFL, Kha and Flambe are popular Haxe frameworks that enable creating multi-platform content from one codebase.[8]

Haxe originated with the idea of supporting client-side and server-side programming in one language, and simplifying the communication logic between them.[9][10][11] Code written in the Haxe language can be source-to-source compiled into ActionScript 3, JavaScript, Java, C++, C#, PHP, Python, Lua[12] and Node.js.[3][6][13][14] Haxe can also directly compile SWF and Neko bytecode.

Many popular IDEs and source code editors have support available for Haxe development.[15] No particular development environment or tool set is officially recommended by the Haxe Foundation. The core functionalities of syntax highlighting, code completion, refactoring, debugging, etc., are available in various degree. The comparison of IDE choices for Haxe programmers has quite in-depth information.

To help leverage existing code, the Haxe community has created source code converters for ActionScript 3 to Haxe[16] and C# to Haxe[17] The Haxe compiler can also output Haxe into standalone ActionScript 3,[18] C++,[19] C#,[20] Java,[21] PHP,[22] Python[23] source code, which can then be pulled out of the Haxe ecosystem and developed with traditional workflows.

Major users of Haxe include BBC, Coca-Cola, Disney, Hasbro, Mattel, Nickelodeon, Prezi, TiVo, Toyota, and Zynga.[24][8]

History[edit]

Development of Haxe began in October 2005.[25] The first beta version was released in February 2006. Haxe 1.0 was released in April 2006, with support for Adobe Flash, JavaScript, and Neko programs. Support for PHP was added in 2008, and C++ was added in 2009. Modern platforms such as C# and Java were added with a compiler overhaul in 2012.

Haxe was developed by Nicolas Cannasse and other contributors, and was originally named haXe because it was short, simple, and "has an X inside", which the author asserts humorously is needed to make any new technology a success.[26]

Haxe is the successor to the open-source ActionScript 2 compiler MTASC, also built by Nicolas Cannasse,[10][27] and is released under the GNU General Public License version 2 or later.[28]

Compiler[edit]

The Haxe language can compile into bytecode for different virtual machines such as the Adobe Flash Player and Neko, and can generate source code in C#, C++, ActionScript 3, JavaScript and Lua

The Haxe compiler is an optimizing compiler, and uses function inlining, constant folding, and dead code elimination (DCE) to optimize the run-time performance of compiled programs.

This strategy of compiling to multiple source code languages is inspired by the write once, run anywhere paradigm. It also allows the programmer to choose the best platform for the job. Typical Haxe programs run identically on all platforms, but developers can specify platform-specific code and use conditional compilation to prevent it from compiling on other platforms.

The following table documents platform and language support in Haxe.

Code generator Output Platform Use Since Haxe version
ActionScript 3[6] source Adobe Flash Player 9+, Adobe AIR Server, desktop 2007 (1.12)
C++ (hxcpp)[6] source Windows, Linux, macOS Server, desktop, CLI 2009 (2.04)
C++ source Android,[29] Apple iOS,[14] Palm webOS[30] Mobile 2009 (2.04)
C#[6] source .NET Framework Server, desktop, mobile 2012 (2.10)
Java[6] source Java Server, desktop 2012 (2.10)
JavaScript[6] source HTML5, NodeJS, PhoneGap Server, desktop, browser, mobile 2006
Neko[6] byte code NekoVM Server, desktop, CLI 2005
PHP[6] source PHP Server 2008 (2.0)
Python[6] source Python CLI, web, desktop 2014 (3.2)
Lua[6] source Lua CLI, web, desktop, mobile 2016 (3.3)
SWF with ActionScript 2[6] byte code Adobe Flash Player 6+ Desktop, browser 2005
SWF with ActionScript 3[6] byte code Adobe Flash Player 9+, Adobe AIR, Tamarin Desktop, browser, server 2005

Language comparison[edit]

Haxe has much in common with ActionScript 3. The Haxe compiler is developed in the OCaml language. No knowledge of OCaml is needed to develop applications using Haxe. Advantages to using Haxe over ActionScript 3 include:

Performance comparison[edit]

The run-time performance of Haxe programs varies depending on the target platform:

  • ActionScript 3: Programs produced using the Haxe compiler usually run faster than programs produced using the Apache Flex SDK ActionScript Compiler.[32] However, using ActionScript Compiler 2 (ASC2) with manual optimizing,[33] many have reported comparable performance.
  • JavaScript: Programs produced using the Haxe compiler run at a comparable speed to handwritten JavaScript programs.[34] OpenFL is a common Haxe-powered framework that can run in HTML5-JavaScript, but content built with OpenFL currently suffers performance issues on mobile devices.[34]
  • C++: Programs produced using the Haxe compiler rival handwritten C++ programs, but C++ applications built with OpenFL suffer major performance issues.[35]

Language[edit]

Haxe is a general-purpose language supporting object-oriented programming, generic programming, and various functional programming constructs. Features such as iterations, exceptions, and code reflection are also built-in functions of the language and libraries.[36] 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.

Since Haxe had its origins in ActionScript 3, all of the existing Flash API can be used, although Haxe requires better-formed code and programming standards than Adobe compilers (for example, regarding scoping and data typing).

Type system[edit]

Haxe has a sophisticated and flexible type system. The type kinds it offers are classes, interfaces, function-method types, anonymous types, algebraic data types (ADTs, called enum in Haxe), and abstract types. Parametric polymorphism is possible with classes, ADTs and function types, giving the language support for generic programming based on type erasure. This includes support for variance in polymorphic functions, although not in type constructors.

The type system is static unless annotations for dynamic typing are present, for use with targets that support them. Type checking follows nominal typing with the exception of anonymous types where structural typing is used instead. Finally, type inference is supported, allowing for variable declarations without type annotations.

Classes[edit]

Classes (keyword class) in Haxe are similar to those in Java or ActionScript 3. Their fields can be either methods, variables, or properties, each static or per instance respectively. Haxe supports the accessors public and private, and more advanced methods for access control (ACL, link) that are denoted using annotations. Methods and static constant variables can be inlined using the keyword inline.

Interfaces in Haxe are very similar to those in, for example, Java.

interface ICreature {
    public var birth:Date;
    public var name:String;

    public function age():Int;
}

class Fly implements ICreature {
    public var birth:Date;
    public var name:String;
	
    public function age():Int return Date.now().getFullYear() - birth.getFullYear();
}

Enumerated types[edit]

Enumerated types are an important feature of the language; they can have type parameters and be recursive.[37] They provide basic support for algebraic data types, allowing the inclusion of product types, in a fashion similar to Haskell and ML. A switch expression can apply pattern matching to an enum value, allowing for elegant solutions to complex programming problems:

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));
    }
}

Examples of parametric enum types are the Haxe standard library types Option[38] and Either:[39]

enum Option<T> {
    Some(v:T);
    None;
}

enum Either<L, R> {
    Left(v:L);
    Right(v:R);
}

Haxe also supports generalized algebraic data types (GADTs).[40][41]

Anonymous types[edit]

Anonymous types are defined by denoting their structure explicitly, using a syntax that follows the mathematical record-based representation of a type. They can be used to implement structural typing for function arguments (see below), and can be given an alias with the keyword typedef:

typedef AliasForAnon = { a:Int, b:String, c:Float->Void };

Function types[edit]

Functions are first-class values in Haxe. Their type is denoted by using arrows between argument types, and between the argument type(s) and return type, as common in many functional languages. However, unlike in prominent examples like Haskell or the ML language family, not all functions are unary functions (functions with one argument only), and in Haxe, functions can't be partially applied per default. Thus, the following type signatures have different semantics than in the aforementioned languages. The type F is a function that takes an Int and a String as arguments, and returns a value of type Float.

The same notation in a language with unary functions only, would refer to a function that takes an Int as argument, and returns a function of type String->Float.

Types F2 and F3 denote the same type. Both are binary functions that return a binary function of type F. For F3 the syntax to declare a function type within a function type is used.

typedef F = Int->String->Float;

typedef F2 = Int->String->F;
typedef F3 = Int->String->(Int->String->Float);

Abstract types[edit]

The latest addition to the Haxe type system is a concept termed abstract types. As used in Haxe, this refers to something different from a conventional abstract type. They are used to make conversions between types implicit, allowing reuse of existing types for specific purposes, like implementing types for units of measurement. This greatly reduces the risk of mixing up values of the same underlying type, but with different meanings (e.g., miles vs. km).

The following example assumes that the metric system is the default, while a conversion to miles is needed for legacy data. Haxe can automatically convert miles to kilometers, but not the reverse.

abstract Kilometer(Float) {
    public function new(v:Float) this = v;
}
 
abstract Mile(Float) {
    public function new(v:Float) this = v;
    @:to public inline function toKilometer():Kilometer return (new Kilometer(this / 0.62137));
}
 
class Test {
  static var km:Kilometer;
  static function main(){
    var one100Miles = new Mile(100);
    km = one100Miles;
 
    trace(km); // 160.935
  }
}

As the example shows, no explicit conversion is needed for the assignment "km = one100Miles;" to do the right thing.

Structural typing[edit]

In many functional programming languages, structural typing plays a major role. Haxe employs it in the presence of anonymous types, using the nominative typing of object-oriented programming, when only named types are involved. Anonymous types in Haxe are analogous to the implicit interfaces of the language Go as to typing. In contrast with Go interfaces, it is possible to construct a value using an anonymous type.

class FooBar {

   public var foo:Int;
   public var bar:String;

   public function new(){ foo=1; bar="2";}

   function anyFooBar(v:{foo:Int,bar:String}) trace(v.foo);

   static function test(){
        var fb = new FooBar();
        fb.anyFooBar(fb);
        fb.anyFooBar({foo:123,bar:"456"});
   }
}

Internal architecture[edit]

Compiler[edit]

The Haxe compiler is divided into one frontend and multiple backends. The frontend creates an abstract syntax tree (AST) from the source code, and performs type checking, macro expansion, and optimization on the AST. The various backends translate the processed AST into source code or generate bytecode, depending on their target.

The compiler is written in OCaml. It can be run in server-mode to provide code completion for integrated development environments (IDEs) and maintain a cache, to further speed compiling.[42]

Targets[edit]

In Haxe, supported platforms are known as "targets", which are Haxe modules that provide access to core-APIs (language and bytecode targets), for the compiler-backends that are responsible for generating the respective code, and for runtimes with specific APIs that go beyond the core language support (platform-targets).

  • Bytecode Targets produce executable byte code (Neko, SWF, SWF8), that can be executed directly by the runtime (Neko VM, Adobe Flash Player, Adobe AIR). Haxe API and platform-specific API is available.
  • Language Targets produce source code (AS3, C++, C#, Java). Most source code must be compiled by a third-party compiler to produce an executable file (Flex SDK, GNU Compiler Collection (GCC), Microsoft Visual C++, .NET Framework, Java compiler). JavaScript and PHP code can be run directly, since the runtime uses just-in-time compilation. Inline code written in the target language can be inserted at any point in the application, thereby supporting the entire platform API; even features missing from the Haxe wrapper API.
  • External Modules are type definitions (extern class in Haxe) that describe the types of native APIs or libraries, so the Haxe compiler can use static type-checking.

See also[edit]

Also on the Haxe platform:

Other languages that compile to JavaScript:

Other multi-platform languages:

References[edit]

  1. ^ "Haxe Download List". 
  2. ^ "Nicolas' announcement of spelling change on Haxe official mail list". 
  3. ^ a b c d Ponticelli, Franco (2008-02-11). Professional haXe and Neko. Wiley. ISBN 0470122137. 
  4. ^ Ivanov, Michael (2011-05-24). Away3D 3.6 Cookbook. Packt Publishing Ltd. ISBN 1849512817. 
  5. ^ Doucet, Lars (2015-06-03). "Haxe/OpenFL for home game consoles". Gamasutra. 
  6. ^ a b c d e f g h i j k l m Introduction to the Haxe Standard Library, Haxe Docs
  7. ^ Target Specific APIs, Introduction to the Haxe Standard Library, Haxe Docs
  8. ^ a b Doucet, Lars (2014-06-24). "Dear Adobe: Support Haxe, save your Tools". Gamasutra. 
  9. ^ "Haxe Interview". Io Programmo. 2009-04-01: 1–6. 
  10. ^ a b Grden, John; Mineault, Patrick; Balkan, Aral; Hughes, Marc; Arnold, Wade (2008-07-16). The Essential Guide to Open Source Flash Development. Apress. p. Chapter 9 (Using Haxe). ISBN 1430209941. 
  11. ^ Fisher, Matt (2013-01-01). HTML5 for Flash Developers. Packt Publishing Ltd. ISBN 1849693331. 
  12. ^ "Hello Lua! - Haxe". Retrieved 2016-07-09. 
  13. ^ "hxnodejs (4.0.9)". Retrieved 2016-07-09. 
  14. ^ a b Haxe, iPhone & C++ At Last, GameHaxe website
  15. ^ List of IDEs supporting Haxe, Haxe Foundation
  16. ^ as3hx, AS3 to Haxe converter, Haxe source code repository
  17. ^ CS2HX - C# to haXe converter, CodePlex project hosting
  18. ^ Haxe Compiler Targets, Haxe docs, "ActionScript 3, Source"
  19. ^ Haxe Compiler Targets, Haxe docs, "C++, Source"
  20. ^ Haxe Compiler Targets, Haxe docs, "C#, Source"
  21. ^ Haxe Compiler Targets, Haxe docs, "Java, Source"
  22. ^ Haxe Compiler Targets, Haxe docs, "PHP, Source"
  23. ^ Haxe Compiler Targets, Haxe docs, "Python, Source"
  24. ^ Companies using Haxe, Haxe Docs
  25. ^ "Haxe 3.2 Release". Github. 
  26. ^ "Haxe mailing list post on naming". Archived from the original on 2007-03-28. 
  27. ^ MTASC Compiler, MTASC website
  28. ^ "Haxe license page". Archived from the original on 2012-05-12. 
  29. ^ "Blog post mentioning Android port progress". 
  30. ^ "How to get started with Haxe 2.06 and the webOS PDK [archived on WayBackMachine]". Archived from the original on October 22, 2014. 
  31. ^ "Haxe introduction page". Archived from the original on 2012-05-04. 
  32. ^ AS3 vs haXe performance, SplashDust website
  33. ^ AS3 Performance Optimization, Starling Wiki
  34. ^ a b Dyachenko, Vadim (2013-12-05). "On "You can't make good HTML5 games in Haxe"". Yellow After Life. 
  35. ^ Kaya, Talha (2014-07-04). "OpenFL & Haxe, A Bumpy Start". Gamasutra. 
  36. ^ "Haxe language reference". Archived from the original on 2012-05-02. 
  37. ^ "Haxe reference detailing the use of enum". Archived from the original on 2012-05-11. 
  38. ^ "haxe/Option.hx at development · HaxeFoundation/haxe". Github. 
  39. ^ "haxe/Either.hx at development · HaxeFoundation/haxe". Github. 
  40. ^ "Language Features". Haxe - The Cross-platform Toolkit. Haxe Foundation. Retrieved 30 April 2015. 
  41. ^ "haxe/TestGADT.hx at development · HaxeFoundation/haxe". Github. 
  42. ^ Server mode command-line: haxe --wait [host:]port
  43. ^ "A Scala to JavaScript compiler". 

External links[edit]