Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Programming Microsoft

Compiling to JavaScript: TypeScript vs. Haxe 94

lars_doucet writes: Released in 2012, Microsoft's TypeScript is perhaps the best-known "compile to JS" language, but it wasn't the first. One of the earliest was Haxe, whose JS target first appeared in 2006. In his illuminating article, TypeScript vs Haxe, Andy Li gives an excellent rundown of the two languages' various merits, but the bottom line is: "Existing JS developers will favor TypeScript as they are more similar in many ways. They can utilize their existing skills immediately. Non-JS developers with backgrounds like Java/C# or even from the functional programming world will appreciate Haxe more since it fixes a lot of weirdness of JS." The full article includes an excellent rundown of the type systems, syntax, scope handling, compilers, and overall language design philosophy.
This discussion has been archived. No new comments can be posted.

Compiling to JavaScript: TypeScript vs. Haxe

Comments Filter:
  • by Anonymous Coward on Wednesday August 05, 2015 @11:51PM (#50260831)

    Compiling to JS is like having a shit for dinner, you're doing it wrong.

    • Re: (Score:1, Flamebait)

      by leptons ( 891340 )

      Compiling to JS is like having a shit for dinner, you're doing it wrong.

      Denigrating JS is like having shit for brains, you're doing it wrong. FTFY.

      • Re: (Score:2, Insightful)

        by spamdog ( 646409 )

        Oh come on. Shitting on javascript is timeless.

        • Horrible syntax for people, pretty decent semantics for compiler backends for a fairly wide class of language. (About the only thing missing at the moment is tail calls.)
      • No, supporting a poorly designed shit language used by shitheads to write bloated shit is rather shitty of you

        • by Anonymous Coward

          You really don't have any concept of why there is javascript. Oh, I forgot - this is slashdot, where you're not a "real" programmer unless you use language XYZ. Fuck off Slashdot, you're all just putting your ignorance on display.

          • There is Javascript for the same reason there is Visual Basic and PHP, to empower dullards and dimwits to be added to payroll rather than quality developers

      • Re: (Score:3, Insightful)

        by Anonymous Coward

        How is this post marked flamebait but not the first original one?
        Javascript is proving to be one of the most widespread and widely adopted runtimes in the world.
        It is cashing cheques that java's ego wrote out.

        Compiling to JS is in fact the way most strong typed addicts are able to write javascript without losing their sanity.

        Mod Anonymous Coward down to flamebait and leptons to informative please!

    • Compiling to JS is like having a shit for dinner, you're doing it wrong.

      What are you talking about? 50 million flies can't be wrong.

      • We're talking about JavaScript, not Windows. :P
        • We're talking about JavaScript, not Windows. :P

          Hey, I was joking; people often make ruder jokes about things they are fond of than about things they dislike. I like Javascript, but I'm not deeply convinced about the merits of compiling something to Javascript. Of course, this sort of idea isn't unknown in other languages, from C++ 'compiling' to C, or C to assembler, or diverse pre-processors like pro C/COBOL/FORTRAN etc - or for that matter IBM's HLASM. They have their place and offer some convenience, but they are not highly relevant to me.

          And don't s

          • I've spent many happy hours watching people struggle with Windows, while I buzz along on my Linux system.

            And I get paid big bucks to fix Windows problems between the keyboard and the chair at my job. Now that's job security.

  • by Dutch Gun ( 899105 ) on Wednesday August 05, 2015 @11:59PM (#50260843)

    How is that web assembly project coming along? It seems like a perfect fit for alternative languages like this instead of having to compile to JS. I think it will be a nice day when developers can choose a web language based on its merits rather than its ubiquitous nature.

    • by thisisauniqueid ( 825395 ) on Thursday August 06, 2015 @12:57AM (#50260989)

      How is that web assembly project coming along? It seems like a perfect fit for alternative languages like this instead of having to compile to JS.

      WebAssembly is not currently a suitable target for Javascript compilation, because it lacks a garbage collector. Only languages that can base themselves on (the equivalent of) malloc/free can currently target wasm. Eventually garbage collection facilities will be provided, but they're not on the immediate drawing board, and even once garbage collection is possible, it's likely to be a long time before JS->wasm approaches the performance of v8 or similar. (Note that asm.js doesn't do garbage collection either.)

      The nice things about wasm are (1) it gives the creators a chance to re-invent the web runtime from the ground up (e.g. native threads will be there almost from the start), (2) languages can evolve independently of the web platform, and the web platform becomes language agnostic, and (3) *all* the major players are fully on-board.

      One of the nicest features of wasm, in my opinion, is that the intermediate representation is an AST of expressions to be computed. This is a far more sensible and flexible IR than some stack-based or register-based VM bytecode system. My prediction is that wasm will become the preferred compilation target for desktop/server applications, not just web applications: the same toolchain will be used to generate binaries for running in server containers as well as within a browser.

      • ...but less powerful? Sounds like a winner to me!

        Another re-invention of the wheel. No one liked JVMs running bytecode in browser applets, I see no reason why this will be any more successful.

        • by Anonymous Coward

          Don't underestimate freedom. The only reason javascript is popular is because every browser has a javascript vm built in. Java was never built in to any browser. Being "built in" is a huge difference. This is not a reinvention of the wheel. It is improving the wheel by removing some of the restrictions of use.

        • It's the future. Go search for interviews with Brendan Eich on the subject of WebAssembly, and you'll see what I mean.
      • by Anonymous Coward

        Funny, when Java was originally released I was only 16 and I read about this "bytecode stuff". For a while, I actually thought it was a compact, syntactical representation of the original Java program code (with comments, variable names etc. removed or replaced by numbers etc.). It seemed to me that would be the optimal way to get fast native code, since as little information as possible is thrown away, meaning that the compiler can better see the high-level constructs and find a good implementation in nati

      • by Pieroxy ( 222434 )

        I agree that the fact that all major players are on board is a plus. That said, this is something we will sadly not be able to target for at least 6 years IMO. The most notable issue being the release cycle of Internet Explorer. If they decide to support their old platforms well, things could go faster. But the fact that we're still stuck with support for IE8 (or were until recently) is due to the fact that it's the newest IE for Windows XP, nothing else.

        • There's a polyfill, so the (very alpha) prototype binary format of WebAssembly works on all Javascript-capable browsers today.
    • Here are my 2 cents on this "web thing":

      The language doesn't matter, the framework matters. I couldn't care less which language I use as long as the APIs are reasonable. But if I have to start considering abhorrences like HTML, encoding data into URLs, "cookies" and dealing with the intricacies of client/server responses, then my reaction is "no, thanks" - unless I'm forced to use the framework anyway.

      In other words, your language could be the shittiest possible and I'd still be fine with it as long as it a

    • "when developers can choose a web language based on its merits rather than its ubiquitous nature." If it's not ubiquitous you don't put it in your site, full stop.

  • by istartedi ( 132515 ) on Thursday August 06, 2015 @12:22AM (#50260893) Journal

    If I had to compile to JS I like to think I could use emscripten [github.com], but not being involved in such a project I'm not sure how well it works. When it's supported by the browser, it's designed to strip away most of the layers of crap we've put between ourselves and the machine. IIRC, you can get half the speed of native C by running C compiled to JS this way. Once again though, the browser has to support it. I think in theory it can support any language that LLVM supports on the front end; but I think they've only tested it with C and C++.

    Anyway, if you're starting a new project I don't see why you'd want to use a language designed just for compiling to JS when you could use something more general purpose that will potentially run very fast with the proper compiler.

  • What about C/C++ and Unreal's scripting system all compiled to JavaScript? Epic has released several demos over the past couple years to show this off. https://www.youtube.com/watch?... [youtube.com]

  • CoffeeScript is the best known and most popular, yes TypeScript probably comes second in these catagories.

    • Exactly what I was thinking. CoffeeScript has 2x the hits on Github or SO as TypeScript making it the clear front-runner.
    • by hatchet ( 528688 )

      But still sucks. Actually all those mentioned compilers suck... and that's because javascript sucks, and you need to change the language to something that is at least strong typed (java, c#, python, ...)

      so you're left with something like: GWT, DuoCode, Bridge.NET...

  • Another one that came fairly early onto the scene was Objective-J [wikipedia.org], which allows programming browser contained applications in the idiom of Cocoa and Objective-C. I haven't really tried it myself, but it looked like a fun curiosity.
  • There are also PureScript [purescript.org], Scala.js [scala-js.org] and of course ClojureScript [github.com].
  • Not that bad (Score:5, Insightful)

    by monkeyxpress ( 4016725 ) on Thursday August 06, 2015 @05:08AM (#50261453)

    I know this is going to get a lot of hate, but JavaScript is really not as bad as people make out. There are some stupid design decisions (mostly around scoping and built-in type consistency) but once you know what these are it is pretty easy to work with them. The main problem I think most people have is that it does not really offer any sort of hand-holding in terms of how you should structure your program. But in a way it is quite beautiful how you can create usable frameworks for OO, imperative, or functional programming with the same language. The problem I mostly see is that people get stuck with a poor design decision, but rather than having to re-factor their object structure (like C# would force you to do), they can just hack in a solution that breaks basically every principle of good programming imaginable. They then dump this solution on the world and declare it is because JavaScript sucks. The real issue is that they are bad programmers who came up with a poor design and then used the (somewhat excessive) flexibility of JavaScript to get them out of a corner. Same thing has happened in C/C++ for years.

    I really don't think JavaScript should have become the language of the web, but competent programmers shouldn't listen to all the hate that gets spread around. It is a decent way to introduce yourself to a lot of new programming concepts if you've come from a static OO language background. Once you understand some fundamentals CoffeeScript, TypeScript etc are pretty straight forward and their applicability is obvious.

    For what it's worth, if you are doing large programs then something like TypeScript can be very useful. The problem it solves is really an issue with dynamic languages in general though, rather than JavaScript. There are ways to create large maintainable code bases with a dynamic prototypical language, but sometimes the classic OO model just fits a problem better.

    • by Anonymous Coward

      The main problem I think most people have is that it does not really offer any sort of hand-holding in terms of how you should structure your program. But in a way it is quite beautiful how you can create usable frameworks for OO, imperative, or functional programming with the same language.

      If that was the problem then people would be complaining a lot more about Lua, which leans way harder on the 'no hand holding, build it your way' thing than JS does.

    • I agree- although javascript has some bad design flaws, it's actually an enormously useful tool for which there is no real replacement.

      For example, how else would one do any kind of dynamic page manipulation? Yes, JS has flaws, but it works fine for what it does. It was never meant to be used to create huge, enterprise applications...I mean, who would write (for example) a medical billing application in JS? No one, because there are better languages for that. But for client-side page manipulation and cal
      • by Raenex ( 947668 )

        Yes, JS has flaws, but it works fine for what it does. It was never meant to be used to create huge, enterprise applications...I mean, who would write (for example) a medical billing application in JS? No one, because there are better languages for that.

        And yet there's Node.js, which is JavaScript on the server side.

        • And yet there's Node.js, which is JavaScript on the server side.

          Yep, I've played with it, and if there's something it can't do I don't know what it is. It's powerful. There's also webkit core which can do some great stuff, but still...I wouldn't want to write anything seriously complex in it. That's probably just my preference. I suppose there really isn't any reason why you couldn't/wouldn't/shouldn't.

          But for client-side stuff javascript seems to be about the only commonly-supported means of getting stuff done. (Yes, there's Flash, but that has its own baggage as w

    • by jjn1056 ( 85209 )

      Agreed, JS is far from a terrible language, just the few nasty bits you have to work around (and there' have been accepted workarounds for years).

      That said I tend to use Typescript now since its a smaller cognitive leap to TS from my strongest language skill.

    • by dwpro ( 520418 )

      The main problem I think most people have is that it does not really offer any sort of hand-holding in terms of how you should structure your program. But in a way it is quite beautiful how you can create usable frameworks for OO, imperative, or functional programming with the same language.

      True. I write straight machine code for the self-same reason; flexibility of writing expansive and divergent frameworks with none of the pesky hand-holding. It's so beautiful!

  • If you think this is would be a good thing, try Nim [nim-lang.org], formerly known as Nimrod. Abstraction level between C and C++ and it compiles to JS. Apparently carries less runtime baggage than emscripten.
  • What I want to know is how does Haxe compare to the javascript compiler in systemd?

The way to make a small fortune in the commodities market is to start with a large fortune.

Working...