Haxe
Paradigm | Multi-paradigm |
---|---|
Developer | Haxe Foundation |
First appeared | 2005 |
Stable release | 3.2.1 / October 11, 2015 |
Typing discipline | Static |
Implementation language | OCaml |
Platform | IA-32, x86-64 |
OS | Windows, OS X, Linux |
License | GPL v2, library: MIT |
Filename extensions | .hx .hxml |
Website |
haxe |
Influenced by | |
ActionScript, OCaml, Java |
Haxe is an open source high-level multi-platform programming language and compiler that can produce applications and source code for many different platforms from a single code-base.[1][2][3][4]
Haxe includes a set of common functionality that is supported across all platforms, such as numeric data types, text, arrays, binary and some common file formats.[2][5] Haxe also includes platform-specific API for Adobe Flash, C++, PHP and other languages.[2][6]
Code written in the Haxe language can be source-to-source compiled into ActionScript 3 code, JavaScript programs, Java, C#, C++ standalone applications, Python, PHP, Apache CGI, and Node.js server-side applications.[2][5][7]
Haxe is also a full-featured ActionScript 3-compatible Adobe Flash compiler, that can compile a SWF file directly from Haxe code.[2][8] Haxe can also compile to Neko applications, built by the same developer.
Major users of Haxe include TiVo, Prezi, Nickelodeon, Disney, Mattel, Hasbro, Coca Cola, Toyota and BBC.[9][10] OpenFL and Flambe are popular Haxe frameworks that enable the creation of multi-platform content from a single codebase.[10] With HTML5 dominating over Adobe Flash in recent years, Haxe, Unity and other cross-platform tools are increasingly necessary to target modern platforms while providing backward compatibility with Adobe Flash Player.[10][11]
Architecture
Unified language
The most unique aspect of the Haxe architecture was the decision to support Flash, JavaScript and server-side scripting with a single unified language.[12][13] In typical web development projects, developers must use many different languages to build an integrated web application:[12][13]
- 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 a single language, and simplifying the communication logic between them.[12][13][14] This means that the application logic does not have to deal with the implementation of the communication, which usually involves working with XML files.
The original goals of Haxe were:[12]
- Build a language more powerful than ActionScript 2 or ActionScript 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 separated into one frontend and multiple backends. The frontend creates of 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 IDEs and maintain a cache, to speed up compilation even more.[15]
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, GCC, Microsoft Visual C++, Microsoft .NET, 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.[16] However, using ActionScript Compiler 2 (ASC2) with proper code design[17] many have reported comparable performance.
- JavaScript: Programs produced using the Haxe compiler run at a comparable speed to handwritten JavaScript programs.[18] 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.[18]
- C++: Programs produced using the Haxe compiler rival handwritten C++ programs, but C++ applications built with OpenFL suffer major performance issues.[19]
Development
Development of Haxe was started in October 2005[20] and 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 humorously asserts is necessary to make any new technology a success.[21]
Haxe is the successor to the open-source ActionScript 2 compiler MTASC, also built by Nicolas Cannasse,[12][22] and is released under the GNU General Public License version 2 or later.[23]
Haxe has much in common with ActionScript 3. The Haxe compiler is developed in the OCaml language. No knowledge of OCaml is necessary 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 only support C++[24]
Source code
The recommended IDE for Haxe development is FlashDevelop,[12] which supports ActionScript 2, 3 and Haxe as first-class languages with syntax highlighting, code completion, and other features.[12][25] Code folding, code refactoring and interactive debugging is also supported within the IDE.[26]
To help leverage existing code, the open-source community has created source code converters for:
To help build applications in various platforms, or to help convert Haxe code into other languages, the Haxe compiler can compile:
- Haxe to ActionScript 3[29]
- Haxe to PHP[30]
- Haxe to C++[31]
- Haxe to Java[32]
- Haxe to C#[33]
- Haxe to Python[34]
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, 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/language support in Haxe. Most Haxe programs will function without changes in any given language, although Haxe programs that use platform-specific API will function only in the given platform.
Code generator | Output | Platform | Usage | Since Haxe version |
---|---|---|---|---|
AVM1[5] | byte code | Adobe Flash Player 6+ | Desktop, browser | 2005 (alpha) |
AVM2[5] | byte code | Adobe Flash Player 9+, Adobe AIR, Tamarin VM | Desktop, browser, server | 2005 (alpha) |
ActionScript 3[5] | source | Adobe Flash Player 9+ | Server, desktop | 2007 (1.12) |
C++ (hxcpp)[5] | source | Windows, Linux, OS X | Server, desktop, CLI | 2009 (2.04) |
C++ | source | Android,[35] Apple iOS,[7] Palm webOS[36] | Mobile | 2009 (2.04) |
C#[5] | source | .NET Framework | Server, desktop, mobile | 2012 (2.10) |
Java[5] | source | Java | Server, desktop | 2012 (2.10) |
JavaScript[5] | source | HTML 5, NodeJS, PhoneGap | Server, desktop, browser, mobile | 2006 (beta) |
Neko[5] | byte code | NekoVM | Server, desktop, CLI | 2005 (alpha) |
PHP[5] | source | PHP | Server | 2008 (2.0) |
Python[5] | source | python | CLI, web, desktop | 2014 (3.2) |
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 functionality of the language and libraries.[37] 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.[13]
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, with regard to scoping and data typing).
Type system
Haxe is statically typed. It has a rich type system that offers classes, interfaces, function/method types, anonymous types, algebraic data types (ADTs, called enum in Haxe), abstract types. Subtype polymorphism is supported via standard, single-inheritance.
Classes, ADTs and function types allow parametric polymorphism based on type erasure, sometimes also called Generics in object-oriented programming languages. Type constructors are always invariant in their parameter types. Bounded quantification can be used for constraining the type parameters of polymorphic functions to subtypes of a set of zero or more types.
Haxe supports both structural typing and nominal typing. To ease the burden on the programmer, without sacrificing type safety, Haxe supports type-inference, which in many cases alleviates the need to write out types explicitly.
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", as well as more advanced methods for access control (ACL, link), that are denoted using annotations. Methods and static constant variables can be inlined using the "inline" keyword.
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
Enumerated types are a key feature of the language; they can have parameters of their own and can be recursive.[38] They provide basic support for algebraic data types, allowing the inclusion of typed tuple values defined with data constructors, 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[39] and Either:[40]
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).[41][42]
Anonymous types
Anonymous types are defined by denoting their structure explicitely, 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
Functions are first-class values in Haxe. Their type is denoted by using arrows between argument types, and the argument type(s) and return type respectively, as common in many functional languages. However, unlike in prominent examples like Haskell or the ML-family of languages, not all functions are unary functions (functions with one argument only), and in Haxe, functions can't be partially applied per default. Therefore, 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
A concept called abstract types is the latest addition to the Haxe type system. The term "abstract type" as it is used in Haxe refer to something different from a conventional abstract type. They are used to make conversions between types implicit, allowing to reuse 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 necessary for legacy data. Haxe is able to automatically convert miles to kilometers, but not in the opposite direction.
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 required for the assignment "km = one100Miles;" to do the right thing.
Structural typing
Structural typing plays a major role in many functional programming languages. Haxe employs structural typing in the presence of anonymous types, using the nominative typing of object-oriented programming when only named types are involved. In the context of Haxe, anonymous types can be thought of as implicit interfaces.
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"});
}
}
See also
Also on the Haxe platform:
Other languages that compile to JavaScript:
Other multi-platform languages:
References
- ↑ "Nicolas' announcement of spelling change on Haxe official mail list".
- 1 2 3 4 5 Ponticelli, Franco (2008-02-11). Professional haXe and Neko. Wiley. ISBN 0470122137.
- ↑ Ivanov, Michael (2011-05-24). Away3D 3.6 Cookbook. Packt Publishing Ltd. ISBN 1849512817.
- ↑ Doucet, Lars (2015-06-03). "Haxe/OpenFL for home game consoles". Gamasutra.
- 1 2 3 4 5 6 7 8 9 10 11 12 Introduction to the Haxe Standard Library, Haxe Docs
- ↑ Target Specific APIs, Introduction to the Haxe Standard Library, Haxe Docs
- 1 2 Haxe, iPhone & C++ At Last, GameHaxe website
- ↑ Dasnois, Benjamin (2011-07-26). HaXe 2 Beginner's Guide. Packt Publishing Ltd. ISBN 1849512574.
- ↑ Companies using Haxe, Haxe Docs
- 1 2 3 Doucet, Lars (2014-06-24). "Dear Adobe: Support Haxe, save your Tools". Gamasutra.
- ↑ Doucet, Lars (2014-03-18). "Flash is dead, long live OpenFL!". Gamasutra.
- 1 2 3 4 5 6 7 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.
- 1 2 3 4 "Haxe Interview". Io Programmo. 2009-04-01: 1–6.
- ↑ Fisher, Matt (2013-01-01). HTML5 for Flash Developers. Packt Publishing Ltd. ISBN 1849693331.
- ↑ Server mode command-line :
haxe --wait [host:]port
- ↑ AS3 vs haXe performance, SplashDust website
- ↑ AS3 Performance Optimization, Starling Wiki
- 1 2 Vadim Dyachenko (2013-12-05). "On "You can't make good HTML5 games in Haxe"". Yellow After Life.
- ↑ Kaya, Talha (2014-07-04). "OpenFL & Haxe, A Bumpy Start". Gamasutra.
- ↑ "Haxe 3.2 Release". Github.
- ↑ "Haxe mailing list post on naming".
- ↑ MTASC Compiler, MTASC website
- ↑ "Haxe license page".
- ↑ "Haxe introduction page".
- ↑ Main Page, FlashDevelop Wiki, "first class support for Flash ActionScript (AS2 and AS3) and Haxe development"
- ↑ Haxe Support, FlashDevelop Wiki
- ↑ as3hx, AS3 to Haxe converter, Haxe source code repository
- ↑ CS2HX - C# to haXe converter, CodePlex project hosting
- ↑ Haxe Compiler Targets, Haxe docs, "ActionScript 3, Source"
- ↑ Haxe Compiler Targets, Haxe docs, "PHP, Source"
- ↑ Haxe Compiler Targets, Haxe docs, "C++, Source"
- ↑ Haxe Compiler Targets, Haxe docs, "Java, Source"
- ↑ Haxe Compiler Targets, Haxe docs, "C#, Source"
- ↑ Haxe Compiler Targets, Haxe docs, "Python, Source"
- ↑ "Blog post mentioning Android port progress".
- ↑ "How to get started with Haxe 2.06 and the webOS PDK [archived on WayBackMachine]". Archived from the original on October 22, 2014.
- ↑ "Haxe language reference".
- ↑ "Haxe reference detailing the use of enum".
- ↑ "haxe/Option.hx at development · HaxeFoundation/haxe". Github.
- ↑ "haxe/Either.hx at development · HaxeFoundation/haxe". Github.
- ↑ "Language Features". Haxe - The Cross-platform Toolkit. Haxe Foundation. Retrieved 30 April 2015.
- ↑ "haxe/TestGADT.hx at development · HaxeFoundation/haxe". Github.
- ↑ "A Scala to JavaScript compiler".
External links
- Official website
- Official Haxe book
- Haxe Learning - Comprehensive Haxe tutorial wiki
- Getting Started with Haxe in FDT 5.0
- haxe.us - an introductory tutorial
|
|