Supriya Ghosh (Editor)

Haxe

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Paradigm
  
Multi-paradigm

Implementation language
  
OCaml

Developer
  
Haxe Foundation

Haxe

First appeared
  
2005; 12 years ago (2005)

Stable release
  
3.4.0 / January 31, 2017; 45 days ago (2017-01-31)

Typing discipline
  
Static, dynamic via annotations, nominal

Haxe is computer software, 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. It is free and open-source software, distributed under the GNU General Public License (GPL) 2.0, and the standard library under an MIT License.

Contents

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. Haxe also includes platform-specific application programming interface (API) for Adobe Flash, C++, PHP and other languages.

Code written in the Haxe language can be source-to-source compiled into ActionScript 3, JavaScript, Java, C++, C#, PHP, Python, Lua and Node.js. Haxe can also emit small web format SWF and Neko bytecode.

Major users of Haxe include BBC, Coca-Cola, Disney, Hasbro, Mattel, Nickelodeon, Prezi, TiVo, Toyota, and Zynga. OpenFL and Flambe are popular Haxe frameworks that enable creating multi-platform content from one codebase.

Unified language

The most unique aspect of the Haxe architecture was the decision to support Flash, JavaScript and server-side scripting with one unified language. In typical web development projects, developers must use many different languages to build an integrated web application:

  • PHP or another server-side language to generate HTML
  • JavaScript to manipulate HTML on the client-side
  • ActionScript for graphical sections built in Flash
  • Haxe originated with the idea of supporting all such aspects in one language, and simplifying the communication logic between them. Thus, the application logic need not deal with the implementation of the communication, which usually involves working with XML files.

    The original goals of Haxe were:

  • Build a language more powerful than ActionScript 2 or 3
  • Be able to easily port an ActionScript application to Haxe
  • Be able to use this language for Flash 6, 7, 8, and 9
  • Be able to use this language for JavaScript-Ajax
  • Be able to use this language for server-side programming, instead of PHP or Java
  • Compiler

    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.

    Targets

    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.
  • Performance

    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.

    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. However, using ActionScript Compiler 2 (ASC2) with manual optimizing, many have reported comparable performance.
  • JavaScript: Programs produced using the Haxe compiler run at a comparable speed to handwritten JavaScript programs. 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.
  • C++: Programs produced using the Haxe compiler rival handwritten C++ programs, but C++ applications built with OpenFL suffer major performance issues.
  • Development

    Development of Haxe began in October 2005. 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.

    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.

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

    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 include:

  • Cross platform abilities
  • A fully developed high-level language
  • Ability to target modern platforms such as C#.NET and Adobe AIR
  • Ability to target devices that support only C++
  • Source code

    The recommended IDE for Haxe development is FlashDevelop, which supports ActionScript 2, 3 and Haxe as first-class languages with syntax highlighting, code completion, and other features. Code folding, code refactoring and interactive debugging is also supported within the IDE.

    To help leverage extant code, the open-source community has created source code converters for:

  • ActionScript 3 to Haxe
  • C# to Haxe
  • To help build applications in various platforms, or convert Haxe code into other languages, the Haxe compiler can compile Haxe to: ActionScript 3, C++, C#, Java, PHP, Python.

    Platform support

    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 ActionScript 3, JavaScript, Lua, and includes experimental support for C++ and C#.

    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.

    The following table documents platform and language support in Haxe. Most Haxe programs will function without changes in any given language, although Haxe programs that use a platform-specific API will function only on the given platform.

    Language

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

    Haxe is similar to ECMAScript, although almost no ECMAScript code will run on Haxe without modifications. Unlike ECMAScript, Haxe is a compiled language. Haxe is inspired by ActionScript, Java, and OCaml.

    Since Haxe had its origins in ActionScript 3, all of the extant 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

    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

    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.

    Enumerated types

    Enumerated types are an important feature of the language; they can have type parameters and be recursive. 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:

    Examples of parametric enum types are the Haxe standard library types Option and Either:

    Haxe also supports generalized algebraic data types (GADTs).

    Anonymous types

    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:

    Function types

    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.

    Abstract types

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

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

    Structural typing

    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.

    References

    Haxe Wikipedia