Grain programming language

Grain is not Elm, but i think Elm programmers will generally like this mostly functional programming language that targets Web Assembly. Hadn’t heard of it until this week.


A nice little language. The only thing I find odd is the way function closures work.
I would expect that a mutable variable gets out of scope once the outer function closure ends. I find encoding a counter this way hideous.

Thanks for sharing, i liked the talk!

Same as OCaml -
let make_counter () =
let t = ref 0
in (fun () → begin t := !t + 1 ; Printf.printf “%d\n” !t end)
let count_up = make_counter ()
in begin
count_up ();
count_up ()

When there’s a valid reference to the anonymous function, and the anonymous function has a reference to this mutable variable, the variable can’t be garbage collected. So parent scope can be kept alive by a nested function.

That is not how closures usually work, they (in most languages) can capture the variables in scope. It works the same way in Elm, but Elm just doesn’t have mutable values (so the counter example is stupid but pure :D)

makeCounter =
    let count = 0
    in \() → count + 1

The function makeCounter will have access to count even after the let scope is over, because the closure closed over the variable.

Grain looks cool, it reminds me to Reason/Rescript but for Webassembly. I think it has a lot of potential, but it is doesn’t have managed side effects like Elm, so it is a bit less strict.

It is awesome seeing all these ML languages popping up, it feels like HM type systems are slowly creeping in to the mainstream, which is great!


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.