Introduction

Haxe is an open source programming language which targets several platforms.

"Platform" definition

Today, most of the available mainstream languages come with their own platform. For instance, the popular Java programming language can be run on the Java Virtual Machine (JVM) platform, PHP can be run using mod_php for Apache, and Javascript source code runs directly on a Web Browser platform such as Firefox or InternetExplorer.

Each platform comes with its own features and benefits : PHP can connect to databases and read/write files, Javascript can manipulate HTML to create runtime interactions into a website, and Flash can be used for video, sound, or games applications.

As a developer, it is hard to develop expertise in all of the existing platforms : there are many of them, each coming with its own programming language, syntax and API, and each being updated on a regular basis with new features.

This is the main reason why most developers feel that they have to specialize in one single technology : either PHP, or Javascript, or Flash, or Java, or anything else that exists. While this enables them, over time, to master a given platform, it also discourages the use of other platforms. The more a developer is specialized and the more he gets used to a given platform, the less likely he will be to try something else, even if it might be the most appropriate technology for the application at hand. Most people are reluctant to restart the whole learning process from "scratch".

Using several platforms

One issue is that sometimes you don't have the choice : today, if you want to use video or sound on the Internet, you would like to use Flash. If you want to create dynamic browser-side interactions, Javascript is also the only available platform for that. If your client can only run PHP on their server, then you will also have to do your server-side functions using PHP. There are many cases where you can't choose your platform.

Looking at it more positively, it's also a very good thing to be able to use several platforms, since it enables you to choose the best technology for every project, or to mix several platforms together to make something creative that would have not been possible with a single technology.

Of course, learning all these different technologies can be very time-consuming, since it requires you to learn a lot of different languages, often using different IDEs or tools, to master the APIs for each set of standard libraries, etc..

This where Haxe comes to help.

Using Haxe

Haxe is a new object-oriented programming language. The language itself is independent of any specific platform : it is not Java, it is not PHP, it is not Javascript, it is Haxe itself. More importantly, Haxe does not have its own platform.

Instead of having its own platform, a Haxe program can be compiled by using the Haxe compiler into another program that will be able to run on the target platform.

For instance, a Haxe program can be compiled into a .js Javascript source file that can later be run into any web browser.

Haxe has many standardized APIs which will run exactly the same on all platforms. This ensures that developers can master a platform independent language while targeting another supported platform.

These standardized APIs include support of Arrays, Lists, Dates, XML, regular expressions as well as sophisticated features like full runtime reflection, abstract I/O, serialization, unit testing and more.

Haxe also has platform-specific APIs that enable you to access the features that are specific to a given target platform. For instance, if you target the Flash platform, you will have access to all the sound, video and graphics API that are available in the Flash Player.

In general, all API libraries that a given platform offers are also accessible from Haxe. The goal of Haxe is to give the developers all the capabilities of a given platform. But keep in mind that since the Haxe application is running on a specific platform, it can't do more than the platform allows.

Haxe is not a framework : it's just a toolbox that you can use to target any supported platform. It does not impose any specific application-design rules, although you can use frameworks written in Haxe to develop your application.

In short, once you learn Haxe, the only remaining job you'll have to do when starting a new platform is to know how to use the platform-specific APIs. This will be much easier than switching platform technology and programing language at once.

Supported platforms

The fundamental idea behind Haxe is to support all the mainstream platforms. This is the reason why Haxe started first with Flash and Javascript support. Since these technologies are installed in the client web browser, there is no possibility to use something else as a replacement.

Let's have a look at all the platforms that are currently supported by Haxe...

Javascript

Javascript is a scripting language that runs on all modern web browsers including Firefox and Internet Explorer. Javascript can be used to "script" websites, for instance to hide/show HTML elements on a webpage depending on the cursor position.

Javascript also allows you dynamically reload only some parts of the web page by using AJAX-style programming.

A Haxe program can be compiled to a single .js file which can be run on a web browser, making Haxe a powerful alternative to programming Javascript scripts directly.

Two major issues with the Javascript technology are addressed by Haxe :

  • the lack of type system : Javascript is a dynamically typed programming language, but does not offer very good debugging capabilities. By using the Haxe static type system, you can safely build and maintain larger Javascript applications, and detect most errors at compile-time instead of runtime. It also enables your IDE to provide code completion while editing Haxe/Javascript programs.
  • the lack of dependencies : in general Javascript programs are grouped into a few .js files, and if you use a JS library you often have to include the whole library while you only use some parts of it. Haxe only compiles the minimal set of classes that you are using into the final .js file, making it as small as possible.

There is currently no huge framework of libraries available as part of Haxe Javascript support, but you can either write extern classes that describe a library classes and directly use them from a Haxe/JS program or write wrapper classes that will use untyped operations in the background, but will be building a consistent API that later can be reused in subsequent Haxe/JS applications.

Flash

The Adobe Flash Player is installed on most Internet web browsers, and is also available on some mobile phones and devices.

Flash provides an effective, simple to use, high speed graphics technology that can be used to develop interactive graphical applications such as games and interactive web-sites. In addition, Flash also provides sound and video playback and recording (using microphone and webcam).

There are three languages and two virtual machines that are part of the Flash technology : the ActionScript 1 and ActionScript 2 languages run on the AVM and the ActionScript3 language runs on the latest AVM2 that was introduced with Flash Player version 9 in 2006 with a brand new API.

Haxe can target both of these virtual machines, which are considered two different platforms called flash and flash9. We recommend using the flash9 platform if you just got started with Flash since it provides a more mature API and better execution speed.

A Haxe program for the Flash platform is compiled to a .swf binary file which can be run by the Flash Player. This makes Haxe a good replacement for programming in ActionScript 1, AS2 and AS3.

Haxe is often considered a better programming language than the most recent AS3 because it provides features such as generics and type-inference. The compiler also runs faster and produces faster code than the AS3 compiler, which makes Haxe a good choice for professional users.

However, Haxe provides several tools to leverage your existing investment in software and to take advantage of third-party Flash and AS3 toolkits and libraries:

  • Haxe can use a .swf library produced by the Flash IDE that contains graphics assets
  • Haxe can also output SWC files, which enable libraries written in Haxe to be used from AS3.
  • Haxe has an AS3 generator which will enable you automatically convert a Haxe program into the corresponding AS3 code.

Another reason that some people prefer to use Haxe is because of its open-source nature, which makes its evolution independent from marketing-driven decisions of Adobe.

PHP

PHP is a popular technology for developing dynamic websites that require server-side programs. It can access databases, filesystems and services available on the server. It typically runs under an Apache web server by using mod_php.

However, in the past years, PHP has incrementally added features coming from more classical Object Oriented languages such as Java, and has became overly complicated because such features were not designed from the start into the language.

If you want to forget about the PHP language quirks, while still be able to run your websites through mod_php, then Haxe might be a good solution for you. The Haxe compiler will compile the Haxe programs into corresponding PHP ones, and will provide you a complete static type system which will enable you to detect most syntax and typing errors at compile-time without the need to visit the website to check if something is wrong or not.

Neko

The Neko Virtual Machine is a lightweight and fast virtual machine that can be easily embedded into any application. The NekoVM runs neko bytecode stored in binary .n files that can be generated by Haxe when targeting the Neko platform.

As part of the Neko libraries, the ''mod_neko" Apache module is provided. This makes it possible to develop the server-side part of a website with Haxe and it on Neko. Neko also provides full filesystem and database access.

The PHP-specific API that is provided with Haxe is also fully compatible with Neko, so you should be able to compile an existing Haxe/PHP website for Neko as well. The main advantage is that the Neko Virtual Machine runs about 30 times faster than the PHP one.

The VM can also be extended with additional libraries written in C, making it possible to access any native technology from Haxe, and to develop full desktop applications in Haxe, or to use Haxe as a scripting language for an application.

Finally, you can easily run a .n file from the command-line or convert it into a binary executable, make also the platform a good choice to develop small command-line tools.

C++

The C++ target is still experimental and should be available starting with Haxe 2.04

A Haxe program can be compiled into several .cpp files that can then be compiled with any C++ compiler and enable you to write desktop applications using Haxe.

The Haxe C++ complier comes with APIs that are compatible with Neko ones, and also an external library which emulates the Flash API, making it possible to run many Haxe/Flash applications with native speed as well.

While the C++ target is still under development, it provides very good performance for all numerical-intensive applications, as well as direct accesses to 3D technologies such as OpenGL.

Platform Evolution

The target platforms will, without doubt, continue to evolve, and it's the job of Haxe to keep up-to-date with this evolution.

We can differentiate the kinds of improvements that a given platform can make as follows:

  • language/syntax improvements : since Haxe has its own language and features, and its own independent evolution, Haxe will not have to keep up-to-date with these changes. In cases where these improvements can improve performance, Haxe will still be able to exploit these in a transparent manner.
  • new apis/features : Haxe should provide access to the new API of a given platforms as soon as they are publicly available.
  • speed/bugfixes : since Haxe runs on the same platform, it will directly benefit from speed improvements or bugfixes that are done.

In some cases, a platform can undergo major changes in its language and/or runtime. The Haxe language has been designed to be able to adapt these changes and the fact that it can already run on all these different platforms is already a proof of its capabilities.

Building cross-platform code

While it's not strictly needed, it can be convenient when you are using several platforms in different projects on a daily basis, to be able to develop code that will run the same on all platforms.

This is already what the Haxe standard library is doing, so as long as your program does only rely on the standard library, it will naturally be cross-platform.

However, if in some cases you want to be able to use platform-specific API, then Haxe has conditional compilation which makes it possible to write code that will compile differently, depending on the target platform.

HaXe is a good way to build your own cross-platform API that you can reuse in your different projects.

version #6067, modified 2009-05-25 16:10:34 by othrayte