Haxe: The Best-Kept Secret in Cross-Platform Development

Haxe, a modern programming language, remains relatively unknown despite its robust features and proven track record since its debut in 2005. Its loyal, albeit less vocal, community attests to its practicality and maturity across various domains, including business, gaming, and academia.

Disney, Hasbro, and the BBC are using Haxe, what contributes to its limited recognition among developers? One possibility is its broad applicability, resulting in the absence of a singular, standout “killer app.”

Another factor could be the niche nature of one of its initial successful applications—providing a migration route from the declining Flash platform. In recent years, the well-established casual games market grappled with uncertainties surrounding Adobe’s future, and it’s now evident that Flash-based solutions will officially have to move by 2020.

The mere mention of “Flash” can lead to disinterest among business software engineers, web developers, and even some game developers. Consequently, the Haxe Foundation rebranded the FlashDevelop IDE, repositioning it as a Haxe IDE, HaxeDevelop.

However, shedding an association, particularly one that remains relevant, can be challenging. For instance, FlowPlay, known for its social games with 75 million users, opted for Haxe over Unity and HTML5 during their recent two-year endeavor to migrate 1.4 million lines of code away from Flash. (A detailed account is available in the case study.)

Balancing the promotion of such use cases while attracting developers beyond the gaming realm might pose a challenge for the Haxe Foundation. Nonetheless, this shouldn’t deter exploration.

Why Choose Haxe?

The Debian project describes Haxe as a “universal programming language.” There are several facets to this.

Fundamentally, the Haxe language emphasizes (good) code reuse. This signifies the ability to reuse Haxe code across multiple platforms, integrate it with existing Haxe and non-Haxe code, and benefit from its support for well-established paradigms like type safety, contributing to code quality.

Continuing the theme of versatility, this translates into several key use case categories, beyond Flash migration.

Building cross-platform apps and games from the ground up. Haxe enables targeting desktop, mobile, and web platforms from a unified codebase. While cross-platform programming languages aren’t novel, Haxe distinguishes itself by targeting not only multiple platforms but also various paradigms, such as HTML5 and native binaries.

One language for all. While reminiscent of Node.js’s promise of a single language for front-end and back-end web development, Haxe extends this concept to any project involving a client-server architecture.

As an example, the web app FontStruct leverages Haxe for both front-end HTML5 canvas drawing and back-end Java2D rendering. (However, this approach is optional; Haxe seamlessly integrates with existing non-Haxe code since it’s designed not to tie you down.) This facilitates consistency in app, game, business, and even rendering logic across diverse contexts, platforms, and output languages.

Escaping JavaScript’s limitations for type safety. While TypeScript serves this purpose within the JavaScript ecosystem, Haxe offers a wider reach, transpiling to Java, C++, C#, Python, and Lua, among others.

Moreover, transitioning from JavaScript to Haxe is relatively smooth, with its syntax avoiding drastic paradigm shifts like those found in languages such as Rebol, despite the latter’s merits. Haxe core developer Dr. Andy Li wrote a more in-depth comparison between TypeScript and Haxe that remains pertinent today, despite ongoing advancements in both languages.

Rapid compiler workflow. A relatively recent addition (though Neko was previously an option), HashLink, a cross-platform virtual machine (VM), balances fast compilation times with runtime performance suitable for demanding applications like 3D gaming. However, even in web development, Haxe can outperform TypeScript both at compile time and at run time.

An exciting and evolving landscape. As an open-source language, Haxe thrives on an active community that continually contributes new features. Its best-kept secret might be its compile-time macro system, which has many interesting use cases, empowering developers to engage in extensive meta-programming. (Examples will be provided later.)

Who’s Using Haxe?

Beyond the prominent presence of game developers—with titles like Madden NFL Mobile, Evoland II, and Double Kick Heroes, among hundreds of others, built using Haxe—Haxe is gaining traction in other domains:

  • In 2014, TiVo employed Haxe to enhance performance on their TiVo Premiere boxes by over 30%.
  • Massive Interactive, serving clients like DAZN and Telecine, utilizes Haxe for their widely-used “smart TV” systems has been using Haxe for years. Philippe Elsass, their UI architect, observed that in his experience with large-scale web projects, Haxe tends to be more straightforward than TypeScript and boasts significantly faster compilation times.
  • Synolia leverages Haxe for their online customization tool, Heidi, which powers solutions for major French brands like Carrefour and La Fnac, as well as Nickelodeon. According to Synolia, Haxe facilitated a smooth transition from Flash to HTML5 while opening doors to new mobile development opportunities. As a SaaS application, Heidi benefits from Haxe’s ability to share code across various application layers and services.
  • Multinational enterprise Docler Holding became a strategic partner of the Haxe Foundation in 2017.

Exploring the Haxe Ecosystem

The Haxe gaming landscape is brimming with open-source frameworks and libraries. From independent developers to established studios catering to international clients, code sharing is prevalent within the Haxe community:

  • Flambe is utilized by major brands such as Disney, Coca-Cola, and Toyota for HTML5 game development.
  • Heaps serves as the high-performance game framework behind the recent 3D strategy hit Northgard.
  • Powering millions of mobile gameplays, the rapid development library awe6 can be considered a hidden gem within a hidden gem.
  • Kha, targeting platforms like Xbox One, Nintendo Switch, PlayStation 4, desktop, and mobile, boasts over 20 game engines built upon it](https://github.com/Kode/Kha/wiki/Engines-using-Kha). This includes Armory, which features seamless Blender integration and recently became open-source.
  • Inspired by the legacy Flash library Flixel, HaxeFlixel is the preferred choice behind popular titles like Defender’s Quest.
  • Gamua’s Starling, the framework used for the Facebook port of Angry Birds, now includes an open-source Haxe port.
  • OpenFL, based on the Flash API, will soon enable targeting multiple consoles, including PlayStation 4, PlayStation Vita, Xbox One, and Nintendo Switch, without additional licensing fees. While HaxeFlixel and Starling are built upon OpenFL, some games, like the award-winning Papers, Please, are developed directly on OpenFL.
  • The Native Media Engine, NME, the origin of OpenFL’s fork many years ago, continues to release major versions.
  • You might recall when HaxePunk, a descendant of FlashPunk, was featured on GitHub’s Release Radar blog.
  • The highly optimized Nape Physics Engine excels in 2D game engines and simulators requiring sophisticated physics.

While game development enjoys significant visibility within the Haxe ecosystem, potentially due to the nature of events like Ludum Dare, the business and enterprise sectors are also making their mark. Consider these examples:

  • The modular app framework hexMachina supports domain-specific languages (DSLs), model-view-controller (MVC) architecture, and numerous other features.
  • The actively developed UI layout engine HaxeUI benefits from corporate backing, with products like 3DVista and Kaizen for Pharma shipping production apps built with it.
  • While not the sole option, the thx.core library and its counterparts provide general-purpose extensions to Haxe, similar to Lodash’s role in the JavaScript world.
  • For JavaScript-targeted Haxe projects, Haxe Modular proves valuable, having assisted both Telecine and FlowPlay in scaling their large projects while maintaining fast client-side loading times.
  • Haxe’s ecosystem continually evolves to integrate with modern technologies, evidenced by the recent addition of a GraphQL library.
  • Lastly, the remarkable Tinkerbell harnesses Haxe’s macro system for a wide array of tasks, offering frameworks for web routing, unit testing, SQL embedding, as well as libraries for templating, CSS selector parsing, async/await, and simplified reactive state management.

These are just glimpses into the diverse applications of Haxe. Haxe.org provides a full list of libraries sorted by popularity that can be explored by tag. Additionally, the Haxe Foundation itself maintains noteworthy projects:

With the benefits outlined, let’s delve into setting up a Haxe development environment.

Getting Started with Haxe

For Win, Mac, or Linux, begin by download Haxe. The installer includes:

  1. The Haxe compiler, enabling you to execute haxe from your terminal or command prompt.
  2. The Haxe standard library, providing low-level and higher-level functionalities, including XML, ZIP processing, and MySQL access.
  3. The Haxelib package manager, facilitating the installation of new packages via the haxelib command. (Note: Explore lix for more advanced package management than Haxelib, particularly in professional settings.)
  4. Neko, a virtual machine target for rapid recompilation and debugging.

(Alternative setup methods exist. For instance, if you have npm installed, OpenFL’s installation instructions offer a Yeoman-based approach. Homebrew and Chocolatey provide similar options.)

While Haxe can be used directly from the command line, IDEs are commonly preferred. FlashDevelop/HaxeDevelop primarily supports Windows, while others are cross-platform (Win/Mac/Linux):

This guide will utilize VSCode. Install the Haxe Extension Pack either via Ctrl+P and ext install haxe-extension-pack or, for minimalists, install just the basic plugin with ext install vshaxe and add other components as needed.

Creating a Haxe Project

Haxe’s transpiling capabilities necessitate a build file for managing target-specific configurations. Initially, a single Haxe class file with a .hx extension will suffice.

Let’s use the Array Comprehension example from try.haxe.org in a file named Test.hx:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Test {
    static function main() {
        var a = [for (i in 0...10) i];
        trace(a); // [0,1,2,3,4,5,6,7,8,9]

        var i = 0;
        var b = [while(i < 10) i++];
        trace(b); // [0,1,2,3,4,5,6,7,8,9]
    }
}

Run Haxe in interpretation mode (without transpiling) from Test.hx’s directory to observe the trace() outputs:

1
2
3
$ haxe -main Test --interp
Test.hx:4: [0,1,2,3,4,5,6,7,8,9]
Test.hx:8: [0,1,2,3,4,5,6,7,8,9]

Success!

Transpiling to JavaScript

To share this code on a web page using JavaScript, execute:

1
$ haxe -main Test -js haxe-test.js

Verify the output in your command line (if Node.js is installed):

1
2
3
$ node my-cool-test.js 
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Alternatively, include the generated haxe-test.js in a web page, and the output will be visible in your browser’s developer console.

Transpiling and Compiling to Native Binary

To create a native binary, transpile to C++ and compile the output. Install hxcpp:

1
$ haxelib install hxcpp

Then, transpile and compile:

1
$ haxe -main Test -cpp bin

Execute the binary:

1
2
3
$ bin/Test
Test.hx:4: [0,1,2,3,4,5,6,7,8,9]
Test.hx:8: [0,1,2,3,4,5,6,7,8,9]

(On Windows, the binary would be located at bin\Test.exe.)

Using Haxe Build Files (.hxml)

.hxml files, despite their extension, aren’t XML. They manage build configurations. Here’s a build-all.hxml file replicating the previous transpilation:

1
2
3
4
5
6
7
8
9
-main Test # tells Haxe our main class is Test (case-sensitive)

--each # all of the above commands will be applied to each target

-js haxe-test.js # our first transpilation target

--next # no other options, time to move to the next target

-cpp bin # our second transpilation (and compilation) target

Note the prefixes: -main, -js, and -cpp are direct haxe parameters, while --each and --next (double hyphens) control parameter usage.

Now, haxe build-all.hxml targets both JavaScript and native outputs.

To transpile to JavaScript and execute with Node.js, use haxe run-js.hxml, where run-js.hxml looks like this:

1
2
3
-main Test
-js haxe-test.js
-cmd node haxe-test.js

For native binary “build and run” (Linux example):

1
2
3
-main Test
-cpp bin
-cmd bin/Test

VSCode’s Haxe extension automatically detects .hxml files, generating build tasks without a tasks.json file, accessible via a dropdown menu.

Note: Be cautious if using multiple VSCode windows open - it might interfere with building via Ctrl+B. Command-line builds remain unaffected.

Haxe 4

The Haxe installer offers both 3.x and 4.x versions. Version 4 brings new features, highlighting the power of Haxe’s macro system. For instance, the lack of short lambda function support was addressed by the community-developed macro library the slambda library implemented support for them via macros. Version 4 incorporates this functionality directly, leading to the library’s deprecation.

Haxe 4 focuses on numerous smaller improvements, reflecting its maturity as a language developed by a dedicated team. Many notable features, like the fast workflow provided by Neko or HashLink, have existed for some time. The compilation server, introduced in 2016, significantly speeds up recompilation for non-VM targets due to in-memory caching, also benefiting code completion in Haxe IDEs.

Haxe 4 specifically brings:

Learning Haxe

While diving straight into the Haxe’s standard API or syntax documentation is possible, there are more beginner-friendly beginner-friendly Haxe materials available as well.

For visual learners, videos from the Haxe US Summit 2018 in Seattle, has its workshops up, along with recordings from previous years, offer valuable insights.

Specific tutorials can be helpful. There’s a start-to-finish tutorial on how to build a dungeon crawler game in HaxeFlixel and a series of HaxeFlixel tutorials that provide in-depth explanations. For 3D development, explore the Haxe tutorial on getting started with Armory.

If you prefer a Haxe tutorial on the fast processing of XML, there are many, some older but still relevant. Haxe’s core concepts remain stable, so tutorials don’t become obsolete quickly. Outdated content usually stems from library dependencies rather than core language changes.


Haxe offers a world of possibilities. Hopefully, this introduction has sparked your interest in this versatile language. Best of luck with your Haxe endeavors!

Licensed under CC BY-NC-SA 4.0