Haxe
Haxe is an open source 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 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. Haxe also includes platform-specific application programming interface for Adobe Flash, C++, PHP and other languages. OpenFL and Flambe are popular Haxe frameworks that enable creating multi-platform content from one codebase.
Haxe originated with the idea of supporting client-side and server-side programming in one language, and simplifying the communication logic between them. 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 directly compile SWF, HashLink and Neko bytecode.
Many popular IDEs and source code editors have support available for Haxe development. No particular development environment or tool set is officially recommended by the Haxe Foundation, although VS Code and IntelliJ IDEA have extensions to support Haxe development. The core functionalities of syntax highlighting, code completion, refactoring, debugging, etc., are available in various degree.
To help leverage existing code, the Haxe community has created source code converters for ActionScript 3 to Haxe and C# to Haxe. The Haxe compiler can also output Haxe into standalone ActionScript 3, C++, C#, Java, PHP, Python and Lua 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.
History
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. Support for PHP was added in 2008, and C++ was added in 2009. More 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.
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.
Compiler
The Haxe language can compile into bytecode for different virtual machines such as the Adobe Flash Player, HashLink and Neko, and can generate source code in C#, C++, ActionScript 3, JavaScript and LuaThe Haxe compiler is an optimizing compiler, and uses function inlining, constant folding, and dead code elimination 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 | source | Adobe Flash Player 9+, Adobe AIR | Server, desktop | 2007 |
C++ | source | Windows, Linux, macOS | Server, desktop, CLI | 2009 |
C++ | source | Android, Apple iOS, Palm webOS | Mobile | 2009 |
C# | source | .NET Framework | Server, desktop, mobile | 2012 |
Java | source | Java | Server, desktop | 2012 |
JavaScript | source | HTML5, NodeJS, PhoneGap | Server, desktop, browser, mobile | 2006 |
Neko | byte code | NekoVM | Server, desktop, CLI | 2005 |
PHP | source | PHP | Server | 2008 |
Python | source | Python | CLI, web, desktop | 2014 |
Lua | source | Lua | CLI, web, desktop, mobile | 2016 |
SWF with ActionScript 3 | byte code | Adobe Flash Player 9+, Adobe AIR, Tamarin | Desktop, browser, server | 2005 |
HashLink | byte code | HashLink VM or HL/C | Server, desktop, mobile | 2016 |
Language comparison
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:- More advanced language features such as extension methods and functional programming
- Ability to target alternative platforms such as C#.NET
- Ability to target devices that support only C++
Performance comparison
- 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 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.
Language
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.
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, 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 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 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
class Fly implements ICreature
Enumerated types
s 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. Aswitch
expression can apply pattern matching to an enum value, allowing for elegant solutions to complex programming problems:enum Color
class Colors
Examples of parametric enum types are the Haxe standard library types Option and Either:
enum Option
enum Either
Haxe also supports generalized algebraic data types.
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, and can be given an alias with the keywordtypedef
:typedef AliasForAnon = ;
Function types
Functions are first-class values in Haxe. Their type is denoted by using arrows between argument types, and between the argument type 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, 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->;
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 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.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
abstract Mile
class Test
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.class FooBar
Internal architecture
The Haxe compiler is divided into one frontend and multiple backends. The frontend creates an abstract syntax tree 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 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, 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.- Bytecode Targets produce executable byte code, that can be executed directly by the runtime. Haxe API and platform-specific API is available.
- Language Targets produce source code. Most source code must be compiled by a third-party compiler to produce an executable file. 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 that describe the types of native APIs or libraries, so the Haxe compiler can use static type-checking.