# Grain programming language

**URL:** <https://discourse.elm-lang.org/t/grain-programming-language/7329>\
**Category:** Learn\
**Created:** [May 8, 2021, 4:28pm UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329 "2021-05-08T16:28:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eelcoh](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eelcoh/32/4909_2.png) [@eelcoh](https://discourse.elm-lang.org/u/eelcoh)\
**Post date:** [May 8, 2021, 4:28pm UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/1 "2021-05-08T16:28:28Z")

</div>

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.

[![](https://img.youtube.com/vi/O8tyml3xBMM/maxresdefault.jpg "Oscar Spencer — Grain: Your WebAssembly-First Programming Language") ](https://www.youtube.com/watch?v=O8tyml3xBMM)

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [May 10, 2021, 1:52pm UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/2 "2021-05-10T13:52:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RyanNHG](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ryannhg/32/5162_2.png) [@RyanNHG](https://discourse.elm-lang.org/u/RyanNHG)\
**Post date:** [May 10, 2021, 11:21pm UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/3 "2021-05-10T23:21:09Z")

</div>

Thanks for sharing, i liked the talk!

---

<div class="post-metadata">

**Author:** ![Donn](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@Donn](https://discourse.elm-lang.org/u/Donn)\
**Post date:** [May 11, 2021, 5:38am UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/4 "2021-05-11T05:38:22Z")

</div>

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 ()  
end

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.

---

<div class="post-metadata">

**Author:** ![joakin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joakin/32/171_2.png) [@joakin](https://discourse.elm-lang.org/u/joakin)\
**Post date:** [May 11, 2021, 10:35am UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/5 "2021-05-11T10:35:50Z")

</div>

> [@Lucas\_Payr](#):
>
> I would expect that a mutable variable gets out of scope once the outer function closure ends

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)

```elm
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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 21, 2021, 10:36am UTC](https://discourse.elm-lang.org/t/grain-programming-language/7329/6 "2021-05-21T10:36:10Z")

</div>

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