Haxe runs on Windows, Mac, and Linux. You can download it here.
What is Haxe?
According to the website:
Haxe is an open-source high-level strictly-typed programming language with a fast optimizing cross-compiler.
So the high level strictly typed programming language makes sense, but a fast optimizing cross compiler? What’s that about?
So I decided to try that out.
I was delightfully surprised to see a bunch of extensions for Haxe in Visual Studio Code.
So the hello world in Haxe is simple:
1 2 3 4 5
The file runs and it outputs this:
1 2 3 4 5 6 7 8
Let’s see if it works. I included it in an HTML file and got the following:
Cool it works!
So I decided to play around with it a little:
1 2 3 4 5 6 7
And then I generated another JS file:
That’s a little crazy, but then I load it up in a browser:
And it works! It’s written the date to the console. Pretty cool.
Let’s try another language.
Hello World in Python
I run it and it generates the following code:
Wow, hello world in only 164 lines of code! great. So I run it:
So let’s run my date code again. I’m using the exact same Haxe file:
1 2 3 4 5 6 7
and it generates this:
Wow. Even more insane code to generate… a date? Well ok. I run it.
Cool! It actually worked with zero modification to the original haxe file.
Instead of the 486 line python file haxe generated I could have simply written:
But I digress. This is an edge case and not a thorough test so I’ll withhold judgment for now, I’m sure haxe has to scaffold a lot of things and do some setup for when you truly use the language to develop a real application.
Let’s try another language.
Hello World in C++
So with C++ and given what I’ve seen so far I have no idea what to expect.
With this one I have to install the hxcpp library, but it’s easy enough:
Then I just change my command a little here:
and after running it, appears to scaffold a bunch of stuff:
and it generates a folder named HelloWorld.cpp:
So I go in and run the exe.
And it works!
again it generates a ton of code:
but again it’s probably just scaffolding for when people build actual applications with it.
The date display works the same:
Creating a Node webserver
So let’s build a little node webserver, I stole the idea from this tutorial
first I have to install the Haxe nodejs library:
Then I create a file named Main.hx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
and then compile (transpile?) it:
And then run it with nodeJS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Which actually isn’t too bad!!
This isn’t a thorough review of Haxe, just playing around. While doing this I didn’t:
- build a real application
- utilize any of Haxe’s advanced language features
- use the Hashlink Virtual Machine
You can see some of the new capabilities of Haxe in this video
What I liked:
-It’s easy to install - It’s available in Windows, Mac, or Linux. I used Arch Linux for this article, at this time the repository still has 3.4.7-1 but I was able to grab the 4.0 binaries, point haxe to the standard library and get going. It took minutes.
-It worked as expected - I generated a lot of stuff just playing around with it, and I didn’t find any “why does this happen” moments. Clearly some work has been put into making this solid, and it shows.
-It’s a really cool concept - I like the idea of using one solid language to generate many kinds of outputs. Some may avoid this pattern but I think if it’s done right it can reduce developer effort.
Why would anyone use this?
So you might be asking yourself why anyone would use this? It’s basically a fancy transpiler right?
Not exactly. While I didn’t really build anything out I did research some of the language features and it appears to be a very mature language with a lot of features I like, such as:
- Classes, interfaces and inheritance
- Conditional compilation
- Static extensions
- Pattern matching
- Anonymous structures
If I were building a real application with this, I’d be tempted to evaluate it based on what I’ve seen.
Remember languages are not meant for compilers, they’re meant for you. Any language that you can get good at and feel productive in is worth a look, and maybe Haxe is the one for you.
Try it out and let me know what you think in the comments!