top | item 8351981

PureScript: a statically typed language which compiles to JavaScript

175 points| bpierre | 11 years ago |github.com | reply

78 comments

order
[+] BinaryIdiot|11 years ago|reply
Seeing these types of experiments are always interesting but just like CoffeeScript it requires learning two languages instead of one [so you an debug] which makes it a hard sale. Why not just write clean JavaScript? I know many want to make JavaScript the assembly language of the web but JavaScript is so much more complex than assembly I'm not sure that's necessarily a good goal to have.

Granted I would love for more languages to be usable on the web; even if I thought JavaScript was the best language it's always good to have competition and alternatives. But I think that has to come through browser support. I wonder how feasible it would be to generate some sort of byte code similar to how Java and others work with the JVM.

[+] Tloewald|11 years ago|reply
I wish a fraction of the effort going into "fixing" javascript went into fixing the real abomination: the DOM API (and CSS). Then everyone would benefit, even people using Dart, Coffeescript, and TypeScript.
[+] paf31|11 years ago|reply
PureScript developer here. Happy to see this here, and happy to answer any questions.
[+] mariusmg|11 years ago|reply
Care for a little comparation with TypeScript ? If i know TS why should i look at PS ?
[+] mhd|11 years ago|reply
With some languages, a big part of the selling point is that it's the same or similar to a language the programmer already uses. Clojure and ClojureScript come to mind, or even server-side JavaScript. Now, with ClojureScript, another common selling point is the synergy with react -- something that would appeal even to people not using Clojure (maybe because they're averse to the JVM).

So where is PureScript positioned related to this? Let's say I'm writing a backend in Ruby or Python and not Haskell, why PureScript instead of CoffeScript or sweet.js?

(I'm actually interested in this, not intended as a put-down.)

[+] alkonaut|11 years ago|reply
Is there any particular reason why PureScript was designed with a single number type (so no proper ints)? I know that's how JS sees things, but now with a whole proper type system it would seem like we should have proper numbers? I admit I haven't read the entire docs, is there a way to e.g. have the type checker ensure a number field such as a year in a date isn't assigned a non integer?
[+] jp_rider|11 years ago|reply
This looks really cool! Do you currently have any abstractions for interacting with DOM elements?
[+] toadkicker|11 years ago|reply
Why do I need statically typed languages to compile to Javascript? Isn't it just better to learn functional programming and dynamically typed languages?
[+] tisue|11 years ago|reply
Saw two great talks about PureScript at Strange Loop last week, one by the language designer, Phil Freeman, and another by Bodil Stokke. Both the language and — almost equally importantly! — the accompanying book seem really well done to me.
[+] agumonkey|11 years ago|reply
Indeed, the book covers a large set of very interesting topics without ceremony.
[+] giulianob|11 years ago|reply
What's the advantage over something like TypeScript which is a superset of JavaScript but supports types as well?
[+] moomin|11 years ago|reply
If you want types like Java, TypeScript's probably a good choice. However, if you want the flexibility or the guarantees a powerful type system gives you, TypeScript's not going to cut it.
[+] paf31|11 years ago|reply
I've written quite a lot of TypeScript, and I generally feel very productive in it, but occasionally I feel like I want more from the type system (type classes, rank-N types, sum types, etc.) and tidier syntax. You might be interested in Bodil Stokke's recent Strange Loop talk, where she discusses why she switched from TypeScript. https://www.youtube.com/watch?v=yIlDBPiMb0o
[+] mck-|11 years ago|reply
Or Dart for that matter?
[+] chowells|11 years ago|reply
The advantage is that types aren't optional. People actually have to respect them.
[+] noelwelsh|11 years ago|reply
Typescript vs Elm? Fight!

(I.e. what are the relative strengths and weaknesses of these two "modern languages targeting JS" offerings?)

[+] gregwebs|11 years ago|reply
I think you meant Purescript vs Elm. disclaimer: I have used neither.

Elm has its own package manager, not sure if that is good or bad!

The biggest difference is that Elm is almost incidentally a similar strongly typed functional programming language and its biggest focus has always been on FRP (functional reactive programming) to simplify UI programming in the browser.

[+] boothead|11 years ago|reply
Just bought the book today ($10 is too low!). After a few nixos hiccups today, so far so good :-)
[+] derengel|11 years ago|reply
From the project docs: "Generate simple readable Javascript" - isn't this more of a disadvantage than an advantage? the created Javascript code won't be high performance, which matters a lot in mobile today.
[+] paf31|11 years ago|reply
"Simple readable JavaScript" as opposed to the sort of output you might expect from compilers for other Haskell-like languages which attempt to preserve Haskell's semantics. Certainly there are cases where using techniques from pure functional programming can lead to poor performance (monadic recursion jumps to mind), but it is perfectly possible to write fast code using PureScript. And if you really need to squeeze out every last bit of performance, you can always write code in JavaScript and use the FFI.
[+] BinaryIdiot|11 years ago|reply
Your minification process should be separate from your build process that's required to run it. This can generate JavaScript that's not a huge pain in the ass to debug, then you as the developer can minify it with existing libraries.

Though I'm not necessarily sure it wouldn't be high performance just maybe a hair slower to load. Then again I don't know how well their generator works.

[+] billpg|11 years ago|reply
I find JS a thoroughly unpleasant language to work with. When will we reach the point where JS could be abstracted away entirely? (Just like how I never need to interact with assembly language any more.)
[+] bru|11 years ago|reply
There are several paths:

1. JS as an assembly language. This is what emscripten[0], a LLVM bytecode-to-JS does, by following the asm.js specification[1]. This is for CPU-intensive tasks, not DOM manipulation. You can use any language that has a LLVM frontend.

2. JS as a target language, from a specific, new language. Those include cleaner variants that keep the same semantics (CoffeeScript, TypeScript, maybe Dart, ...) or more innovative ones, such as Elm[2], which is written in Haskell too. I'd say that a big innovation that makes the life easier is FRP (which is at the heart of Elm or React.js).

I fail yet to see what advantages PureScript brings compared to Elm.

0: https://github.com/kripken/emscripten 1: http://asmjs.org 2: http://elm-lang.org/

[+] rattray|11 years ago|reply
In what ways does CoffeeScript not suit you perfectly?
[+] cmdrfred|11 years ago|reply
I'm new to this but can someone enlighten me about what is the advantage of using this over pure JS?
[+] gamegoblin|11 years ago|reply
Type correctness can help eliminate an entire class of bugs (type mismatch bugs) found in dynamically typed languages, or various gotchas with implicit typecasting.
[+] lemming|11 years ago|reply
One thing I couldn't see anywhere - is PureScript strict rather than lazy?
[+] paf31|11 years ago|reply
It uses JavaScript's evaluation model - i.e. strict, but you can write lazy code using libraries, such as purescript-lazy.
[+] sassyalex|11 years ago|reply
Great language, but javascript code is clean, why not only keeping javascript style and adding typechecking and es6 by default..
[+] andrewchambers|11 years ago|reply
Javascript being clean is your own (possibly misinformed) opinion.
[+] NaNaN|11 years ago|reply
Markdown code and JavaScript code are both clean. Now people know how to "kill" JavaScript, but it may become the new assembly language.
[+] pjmlp|11 years ago|reply
Looks nice, but my employer will never use anything other than JavaScript as browser language, unless a customer asks to do so.
[+] lmm|11 years ago|reply
Get a better employer. I can understand thinking that e.g. existing options are not mature enough, but "never" is a stupid policy.