Hacker News new | past | comments | ask | show | jobs | submit login
Hello Lua (haxe.org)
226 points by ponticellif on April 29, 2016 | hide | past | favorite | 70 comments



Haxe is one of the best kept secrets of the programming world. Created by none other than the author of curl, it's A C-ish, javascripty, but strictly typed language with almost infinite portability. It "compiles" to many different target languages, from PHP to Java to Python to C++ and more. It's fast, has a beefy standard library, well-documented system interfaces, and just feels fun. A remarkable accomplishment.


I agree completely! ... Well, almost agree completely.

Daniel Stenberg is the author of curl: https://daniel.haxx.se/

Nicolas Cannasse is the (main) author of haxe: https://medium.com/@ncannasse

Both are awesome in their own right.

I wrote the Lua target announced here, and am happy to answer any questions.


Thanks for the authorship correction! Sorry, Daniel Stenberg, you complete stud!


Can you explain who is the target audience for Lua target? What can I do with it?


Well, with LuaJIT you get speed.

How much speed?

CloudFlare's firewall system uses LuaJIT to analyse approximately 4 million GET requests per second.

(Small pause, reread)

That much speed. That was the figure in June 2015, at least.

Some of the autogenerated code looks like this: http://i.imgur.com/F5aT0NF.png (snagged via pdfimages from http://www.slideshare.net/jgrahamc/lua-the-worlds-most-infur...)

This video was where I had my major TIL moment: http://www.thedotpost.com/2015/06/john-graham-cumming-i-got-...

I also got the impression from the one other page of autogenerated code briefly visible in this video that the firewall logic is also full of regular expressions. And yet, it completes its analysis in roughly less than 1 millisecond.

It's almost a crime I haven't learned LuaJIT yet. :P

That said, looking at the slideshare link, you can see that Lua as a language has some pretty annoying syntax, so I've sorta been shying away from it.

So maybe I'll learn HaXe instead. :D


> CloudFlare's firewall system uses LuaJIT to analyse approximately 4 million GET requests per second.

While I agree with your premise (LuaJIT is a pretty awesome project), this example means nothing in terms of performance.

— "analyse" doesn't give an indication of the workload -- it could vary from "matching against a precompiled regexp" to running advanced statistical models.

— 4 millions requests per second does not mean anything without knowing the size of the deployment. You can always throw more hardware at a "trivially" parallelizable problem.

— Throughput is only one dimension on the "speed" continuum, latency (and especially tail latency) is also very important. Some languages have very good throughput and fail hard at tail latency (Java, Go until recently). Some others have lower throughput but more predictable latency.


You make incredibly fair points and I don't have the data to answer what you've raised. This could be lack of research or the fact that CF hasn't released it. I expect a 20/80 non-researched/non-available mix.

My understanding is that the WAF detects trivial stuff like SQL injection and XSS, and the video also mentions it detected ShellShock and so forth (yeah, this was a while ago).

"Thousands of regular expressions" is mentioned verbally in the video around ~11:25. That's where I got that from.

There are some bits mentioned about the hardware around 19:30:

- E5-2630v3 (16 cores, 32 w/ HT which is turned off), 128GB RAM, 10Gbps

- Custom built by Quanta: 4 nodes per 2U (each node is like half-width and 1U high), redundant 2xPSU in the middle

- 40 machines run Kafka storing 400TB of log data (ingest @ 15Gbps), 50TB disk per node

- 5 machines run CitusDB (sharded PostgreSQL) to power realtime graph generation, 12TB SSD per node

- 100+ machines for realtime analytics running Go (and presumably LuaJIT) - LuaJIT seems to be the realtime "hmm, interesting" flagging system, while Go is used for in-depth intelligent analysis that's (I assume) slightly behind realtime.

Also, the video is actually over here - https://www.youtube.com/watch?v=LA-gNoxSLCE

---

I'm curious to understand what tail latency is, and very interested to learn more about language latency and performance in general. What subjects would you recommend I learn about / read? (I would describe myself as interested from a hobbyist-intermediate standpont, but from a non-academic POV; I unfortunately get dismally lost with formulaic presentations, math is not a strong subject for me.)


> While I agree with your premise (LuaJIT is a pretty awesome project), this example means nothing in terms of performance.

Yup, without knowing deployment details etc this number doesn't really mean much.

perhaps you might have heard of the snabb-switch project ? which does line rate 10gbps forwarding of 64b packets on a single x86 core. entire thing is written in lua including the user land 'drivers' for Intel 10g cards :)

Edit: s/shabby/snabb (damn you autocorrect)


Wait, wait. Lua or LuaJIT? That's amazing!


> Wait, wait. Lua or LuaJIT? That's amazing!

from 'user' perspective you are still writing lua code. lua-jit would be an optimizing compiler doing it's magic no ? get the code from here : https://github.com/snabbco/snabb


The README says it uses both Lua and LuaJIT, and I can see clear references to LuaJIT in the codebase.

I'd heard of snabb a little while ago (I think in the context of Erlang), but this only makes it even more interesting. Thanks for the pointer!


My guess would be for current lua embedding scenarios - for example for use as a scripting language in games.


You can build a pretty damn good simulation of a Boeing 737-300 with it. (http://ixeg.net/)


Meanwhile, Daniel also did a C-like language back in the day: FPL. It's never seen widespread use, but was the macro-language for his editor, FrexxEd, for the Amiga.


Lua is still my goto embedded scripting language. I don't have any experience with haxe per se, but is the transpiler embeddable as well?


The compiler is written in ocaml, so I'd guess not.


Haxe is what ActionScript should have been. However (imo), it's hard to find a niche for it now. What does it get me that Perl and OCaml don't? I've been drawn to Haxe several times over the years, but have never really thought of a project, "huh, Haxe would be a great fit here". Maybe if I wrote a mobile game or something.


Haxe is really a formidable little virus. It does a lot of small things nicely, but doesn't have a single knock-out feature that you can trot out during language comparisons.

However, all those little features add up over time and become addictive. You start really missing them when you go back to other languages. In the back of your mind you think of how you might solve something simply with abstracts or macros, or how you might easily reduce the file size or execution speed with dead code elimination or inlining.

I would choose Haxe over Perl because of Haxe's expressive type extension capabilities. I could add useful methods to string or array types, and make the resulting code short and sweet (although perhaps less readable for others).

I would choose Haxe over OCaml and I would gain Haxe's superior compiler error messages. I would also get to move seamlessly between functional and imperative programming styles, and still get to use ADTs.


OCaml accommodates imperative programming just fine: `if` without `else`, `for` loops, single semicolons as statement delimiters (without enclosing statement blocks in parentheses), records and objects containing mutable data, etc. OCaml isn't like SML and Haskell, which <strike>do get in your way when you want to program imperatively</strike> push you a little bit more forcefully towards programming in a functional style.

On the other hand, Haxe lacks features like higher order modules and signature ascription, which give the programmer very fine-grained control over the scope of abstractions. Haxe's abstract types are more like what ML had in the 70's, before MacQueen invented the ML module system.


OCaml provides some nice syntactic sugar for imperative programming patterns like "if" statements. But, I wouldn't really call it an imperative language, as it has its roots firmly in ML (which is not such a bad thing after all).

Arguing that ML had a precursor to Haxe abstract types is well within reason, but I'm not so sure they're the same. I'll have to do some more reading.


The reason we started using it at work was we could target both Javascript and Flash from the one codebase.

However, we finally don't need to support flash anymore... so now there's no reason to not just write in javascript.


I find Javascript is quite a good reason not to write in Javascript, but YMMV...


Haxe is still an excellent hedge. Js doesn't offer the same breadth of deployment targets.


from what little i've seen of haxe (used it for a few toy projects), it seems a lot more pleasant to program in and to maintain than javascript.


cURL was created by Daniel Stenberg, Haxe was created by Nicolas Cannasse. (EDIT: beaten)

My favourite Haxe feature is compile-time macros. This leads to massive meta-programming and language extension capabilities. My least favourite aspect is that it does not strictly unify or abstract every target language & runtime, so you end up writing in that language's semantics but using Haxe as syntax sugar. (Good for C++, less useful for other targets.)


Can you explain what you mean by "not strictly unify or abstract every language & runtime"?

In day-to-day use, how would you notice this problem? Can you give an example of what problems or hurdles this can cause?

I've only used Haxe briefly for a few small game demos, and loved it, but I've only used the JS backend.


Presumably they mean that porting your js examples to C++ isn't likely to be as easy as just flipping a switch.

Still orders of magnitude less difficult than rewriting the whole thing in another language, and given that a big target is games, abstracting over the platform semantics would be too costly IMO.


Some targets will have different behaviours regarding null[1], mostly for static language targets; e.g. basic types that are represented by primitives cannot be null when targeting C#, Java, Flash, or C++. Dynamic language targets don't have this issue. (Can be avoided by wrapping your values in Null<T>).

Overflow is normally not handled consistently[2]; it's left up to the target platform to define the overflow behaviour. E.g, JS only has one numeric type (a 64-bit floating point), and can't represent integers over 52-bits. Can be avoided by using Int32 and Int64 classes, but this reduces the code interop if you want to export a library (consumers would need to make use of the Haxe representation of a 64-bit integer).

Arrays aren't fixed sized; if you want this, you need to use Vectors[3], which aren't interface-compatible with Arrays, so you can't use them as a drop-in replacement. (Compare this to Java's Collections API.)

File system access isn't available on every platform, e.g. JS, Flash. If you target Node.js you can use the file system, you just can't use the normal file API, so you need to write stuff specific to each target.

Dates are, as always, a pain to deal with. Timezones aren't supported by the Date class[4]. DateTools.makeUtc() is not available on some platforms[5].

The built-in database API is only accessible on C++, Neko (Haxe-specific bytecode/runtime), and PHP[6]. AND it only seems to support MySQL. This has stymied my ideas about a cross-language multi-DB ORM.

Basically, you have to intimately know the gotchas of every language you target, and if you already know them you may as well write in that language.

Having said all that, Haxe seems to be greatly improving these abstractions with every release; I think given enough time, it will become an amazing tool. I'm starting to use it in place of Python as my "go to" language. For most purposes you can just use a single target; Neko works well for most functionality, but you can always target C++ for "true" cross-platform compatibility and performance, and JS for the browser.

[1] http://haxe.org/manual/types-nullability.html

[2] http://haxe.org/manual/types-overflow.html

[3] http://api.haxe.org/haxe/ds/Vector.html

[4] http://api.haxe.org/Date.html

[5] http://api.haxe.org/DateTools.html

[6] http://api.haxe.org/sys/db/Mysql.html


Thanks for an excellent list of examples!


Also, HaXe is the only viable technology which enables a high-quality canvas-based applications which mirror native applications (while also permitting compilation to those native applications).


I suppose you're not counting the likes of NW.js and Electron because the native version isn't really native - though from the user's perspective, it can feel as native as a Haxe application, assuming they don't look at the ridiculous distributable size and RAM usage of the NW version.

Even then, this still leaves C/emscripten which can also fulfill those requirements, and with similar output sizes (I've tested both and compared the sizes of the resulting web versions, seems that both have an overhead of 1-2 MB for a small graphical application). It depends on what you're planning to do, but one problem I found with Haxe is that it's lacking a good general-purpose UI library that also works well on the web target. Only lower-level graphics libraries.


Wait which curl are we talking about because I'm not sure Daniel worked on Haxe. There is also Curl the programming [1] language (which reminds me of Haxe's goals) but I don't think the creator of that worked on Haxe either.

[1]: https://en.wikipedia.org/wiki/Curl_(programming_language)


You might be also interested in https://github.com/alehander42/pseudo-python . It's still in very early stages, but some of it goals overlap with Haxe-s


> "compiles"

* transpiles


"A compiler is a computer program (or a set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language)" [0]

Haxe does everything a typical compiler does, right up until the very last step. Not much point in correcting people about the use of the term.

[0] https://en.wikipedia.org/wiki/Compiler


Sorry for off topic, just a heads up for the maintainers of haxe.org. Several links in the footer, inside 'Learn Haxe' section are broken. They point to '/http...'.


Can confirm. "API Documentation", "Try Haxe", "Haxelib", and "Code Cookbook" all start with href="/http://".


Also, on most websites, the logo/brand in the top left is a link to the homepage. I clicked it a couple times until I saw the "Home" link next to it.


I've been trained out of this because many company blogs link to the root of the blog, and not back to the company product website.

I don't bother clicking that logo anymore.


Yeah, I hate that too.

I remember seeing a random website the other day, the blog had a homepage link (not in the image, as "Home" text). That was really nice.


Yeah, this Haxe blog has that. It is nice.


That's true. I find that annoying as well. It wasn't immediately clear to me that this page was a blog though. They do have a link to the homepage in the menu at least.


Well this has just introduced me to Haxe in general. This looks frankly, amazing. Where has it been all my life.


If you have a piece of business logic that you want to make available in N languages/platforms, is Haxe a valid option? The only other alternative I know is to write a library in C/C++/Rust and use the FFIs in your target languages to call it.


Definitely.

I use it for some game development projects and its able to compile and run code on all Windows/Mac/Linux/iOS/Android (all native code) and even Javascript+WebGL.

Obviously the framework I use is targeted to games but there are other frameworks that target webdev (can target Node.js/PHP and lots of other things).


Obviously the framework I use is targeted to games

Which one is that, and how well does it do cross-platform?


The framework is Luxe [1][2] and it/Haxe support the platforms I listed above really well (there are published/commercial games made with it on all platforms).

If you have any questions about it, their Gitter/chat [3] is pretty active.

[1] https://github.com/underscorediscovery/luxe

[2] http://luxeengine.com/docs/

[3] https://gitter.im/snowkit/public


If Haxe had an interpreter (not hscript as it is far too limited) I would use it for a lot. Now compiling takes too long. The JS version is quite fast but not good for everything. It's a fantastic product though; in my experience it is write once - run anywhere. With some tweaks but that's to be expected. Please someone implement an interpreter in Haxe for Haxe.


Is Neko what you'd be looking for? Or a completely interpreted version?


Haxe has an interpreter mode if you use --interp as a target

I haven't used it so I'm not sure how powerful it is


From Haxe's introduction:

> Its syntax largely follows the ECMAScript standard, but deviates where necessary.

Anyone can tl;dr the main differences to JS?


It's much closer to ActionScript and ES4 than to ES5 or ES6/2015/Next/Harmony/Trickshot.LegitScript/whatever the latest ECMAScript standard is. The main difference is probably decent macro support.

There's always the Cookbook (http://code.haxe.org/category/beginner/index.html) if you want to see for yourself.


Off the top of my head, Haxe has:

  block level scoping (vs. function level scoping)
  classic inheritance (vs. prototypes)
  distinction between hash and object (vs. nope)
  required semicolons (vs. still uncertain on that after 10 years)
There's also the whole raft of static typing features, but there's too many of those to mention.


ES has all of those things except for requiring that a statement ends with a semicolon.


I would disagree that ES has classical inheritance. ES6 has syntactic sugar over prototypical inheritance that resembles classical inheritance in other languages, but it's still prototypical under the hood.


Is there a meaningful ? I.e., what does classical inheritance get you that prototypes-under-the-hood don't? For a programmer using classes, that ES's are implemented using prototypes should be as important has how the JVM works.


Classical inheritance enforces that a method can only be overridden by another method in a subclass.

Prototypical "inheritance" allows different types of values to be assigned to the same symbol.

E.g. if you have a method bob() on a prototype, you can set object.bob = 'astring'; and it isn't an error.


That has more to do with a language opting out of having fixed types than it does with class-based versus prototype-based inheritance.


Strong static typing with a fairly powerful type system, pattern matching, operator overloading, compile time macros, inline functions, in addition to many others.

Personally my favorite feature is its abstract types, which allowed me to define a 0-overhead color abstraction the was just a 32 bit RGBA integer everywhere. Stuff like this can have a sizable performance benefit for games by reducing GC pressure.


Basic for loops:

    for(x in 0 ... 5) {
        // increment by +1 only
    }
More verbose JSON:

    var t = Json.parse("{ x: 1 }");
    var tx = t.x; // just might be undefined
    // vs working
    var tx2 = Reflect.getProperty(t, "x") 
Tricky array sorts:

    myArr.sort(function(a, b) {
        // just might crash
    });

    // vs stable
    haxe.ds.ArraySort.sort(myArr, function(a, b) {
    });
More verbose everything:

    var v1 = 0;
    var v2 = 0;
    var v3 = 0;
    
    var v4 = function(a, b) {
        // also no 'arguments', that's more verbose too
    };
Different syntax for familiar libs (if supported):

    https://github.com/andyli/jQueryExternForHaxe

    new JQuery("li").hide(); // $("li").hide()
If you're not targeting JS your HTTP requests are probably going to be synchronous and block the main thread.

If your target is only JavaScript / NodeJS you can get a lot of the benefits of Haxe by using TypeScript, with a much larger ecosystem to help you achieve your goals quickly.


Another reason to make 2d games with haxe! It already has some great frameworks and libraries for it. So its great from a practical perspective if you want to build something like that.

I have found the language to be a bit quirky. Its powerful but an odd combination ocaml features and actionscript cruft.


I did not get around to use haXe in a production env yet but it is one of the very few language/ecosystem/target areas where there has been a huge amount of both innovation and consolidation during the past n years.

Shouts to Nicolas and all contributors!


Lua is a great language


i concur


Does anyone think Haxe is a good server-side language? I'm still desperately thirsty for a server-side language that has a sound, yet understandable and easy-to-use static type system.


Yeah, I think so. The most common server-side usages of Haxe are the php, node.js, C++, and neko targets.

UFront is a good example of a server-side usage for Haxe: http://ufront.net/


What about Go?


Haxe seems an interesting alternative for mobile development ( cross platform) for my currently chosen way : Xamarin.

Any tutorial for a crossplatform app on haxe with using native features ( eg. gps)?


Haxe is cross platform in the sense of being able to compile to different languages not in the sense of merging different platform features into one library.

For example, you can use Haxe to build a native Android application using the Java target as you would do with using Java in the first place. Same with iOS (but using the C++ target)

AFAIK, there isn't any active project that offer the same as Xamarin in Haxe. You could, in theory, use Haxe to target C# and Xamarin but I haven't tested it.


It's transpilers all the way down.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: