# Leaf - A scripting language for Elm

**URL:** <https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727>\
**Category:** Show and Tell\
**Created:** [September 2, 2021, 6:58pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727 "2021-09-02T18:58:55Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [September 2, 2021, 6:58pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/1 "2021-09-02T18:58:55Z")

</div>

I’m proud to annouce Leaf ([Orasund/leaf-lang](https://package.elm-lang.org/packages/Orasund/leaf-lang/latest/)) - A multi paradigm scripting language for Elm. It is designed to be extendable to fit your needs.

Leaf is

- dynamically typed
- extendable (by normal Elm functions)
- based on Lua with a few features taken from Rust
- context sensitive (similar to Ports in Elm)
- small (50KB of pure Elm code)

Checkout the [interactive documentation 😉](https://orasund.github.io/leaf-lang) I’ve spent twice as much time on the documentation as on the actual implementation, so I hope you like it.

# Future Plans

The next step will be to work on a package to write nice error messages like in [spectreconsole/errata](https://github.com/spectreconsole/errata) or like in the Elm compiler.

My future plans with Leaf are mostly to keep improving the documentation. I have no plans to add features to it - It’s extendable for a reason. I have a few ideas how one could add additional types (like Maybe or Result) but these will properly stay private projects.

---

<div class="post-metadata">

**Author:** ![thetechnaddict](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/thetechnaddict/32/3721_2.png) [@thetechnaddict](https://discourse.elm-lang.org/u/thetechnaddict)\
**Post date:** [September 4, 2021, 11:19pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/2 "2021-09-04T23:19:31Z")

</div>

I don’t understand the ‘why’ - What problem does this solve?

---

<div class="post-metadata">

**Author:** ![francescortiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/francescortiz/32/1721_2.png) [@francescortiz](https://discourse.elm-lang.org/u/francescortiz)\
**Post date:** [September 5, 2021, 6:48am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/3 "2021-09-05T06:48:39Z")

</div>

I guess you ask why scripting and not why leaf. In my case, I use scripting (schelme) in order to provide serializable customization of some calculations in the UIs.

---

<div class="post-metadata">

**Author:** ![pd-andy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pd-andy/32/4145_2.png) [@pd-andy](https://discourse.elm-lang.org/u/pd-andy)\
**Post date:** [September 5, 2021, 7:19am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/4 "2021-09-05T07:19:17Z")

</div>

I’d love to see an example of this in the wild if you have anything OSS.

---

<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:** [September 5, 2021, 9:35am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/5 "2021-09-05T09:35:42Z")

</div>

The reason I needed such a language is that I’m building a card-game. In this game, the users should be able to add their own cards. To do so, I need a scripting language.

I needed a language that was very accessible, - scheme and lisp don’t do the job. That’s why I decided to write my own.

---

<div class="post-metadata">

**Author:** ![klemola](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/klemola/32/4593_2.png) [@klemola](https://discourse.elm-lang.org/u/klemola)\
**Post date:** [September 6, 2021, 6:06am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/6 "2021-09-06T06:06:33Z")

</div>

This is great work! The docs are nice, and hopefully I’ll find the time to try Leaf with my traffic simulation project, [Liikennematto](https://github.com/klemola/liikennematto).

I have been thinking about how to implement “game levels” or save games. The idea is to allow someone (at least myself) to easily create tilemaps and place cars to the world.

I have a World record that holds everything that changes over time, like cars, the road network and buildings. I’m currently using plain Elm fixtures to build the `World`s by hand, and those fixtures are used in tests. It’s a bit cumbersome. I would like to choose one of those fixtures or other pre-defined scenarios (which savegames happen to be, too) in Liikennematto by using some kind of menu.

---

<div class="post-metadata">

**Author:** ![nkrisc](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@nkrisc](https://discourse.elm-lang.org/u/nkrisc)\
**Post date:** [September 8, 2021, 4:13pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/7 "2021-09-08T16:13:46Z")

</div>

Very cool. One small suggestion for the docs: if you mean for this to be very accessible, I would consider not using ligatures in the doc font to turn `->` into an arrow, it could confuse some if they’re not already familiar with the convention.

---

<div class="post-metadata">

**Author:** ![mika](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mika/32/616_2.png) [@mika](https://discourse.elm-lang.org/u/mika)\
**Post date:** [September 17, 2021, 5:42pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/8 "2021-09-17T17:42:20Z")

</div>

Nice work! I am trying to use this for a random thing and got stuck on syntax. I feel I tried everything and looked through the source but for the life of me I can not understand how accessing values in objects work. 😃

So:

```auto
mut a = { helloWorld : null,
  favoriteNumber : 42
};

a.favoriteNumber

```

→ `Parser error: Expecting End at row:5 col:2`

How would I access the value in key `favoriteNumber`?

---

<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:** [September 17, 2021, 6:11pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/9 "2021-09-17T18:11:40Z")

</div>

Hi there, Leaf does not come with a standard lib, so you have to write the getter yourself:

```elm
get : String -> Dict String Value -> Value
get field obj =
    obj
        |> Dict.get field
        |> Maybe.withDefault NullVal

main : Html msg
main =
    let
        context =
            [ get
                |> Leaf.binaryFun (Leaf.typed Leaf.asString)
                    (Leaf.typed Leaf.asObject)
                |> Leaf.field "get"
            ]
                |> Dict.fromList
    in
    Html.text <|
        case Leaf.run context script of
            Ok ( value, _ ) ->
                value |> Leaf.toString

            Err err ->
                "Error:" ++ err

```

[(Ellie)](https://ellie-app.com/fk52YJK8GXva1)

---

<div class="post-metadata">

**Author:** ![mika](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mika/32/616_2.png) [@mika](https://discourse.elm-lang.org/u/mika)\
**Post date:** [September 18, 2021, 9:03am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/10 "2021-09-18T09:03:08Z")

</div>

Thanks. Was expecting this to be in be in there for some reason. 🙂

---

<div class="post-metadata">

**Author:** ![mauro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mauro/32/3631_2.png) [@mauro](https://discourse.elm-lang.org/u/mauro)\
**Post date:** [September 20, 2021, 10:32pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/11 "2021-09-20T22:32:05Z")

</div>

> Leaf is dynamically typed (just like JavaScript), this means values have types, but variables don’t.

Nice way of thinking about it. The docs are sublime ✨

---

<div class="post-metadata">

**Author:** ![mauro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mauro/32/3631_2.png) [@mauro](https://discourse.elm-lang.org/u/mauro)\
**Post date:** [September 20, 2021, 10:37pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/12 "2021-09-20T22:37:22Z")

</div>

I don’t understand why this would return true

```auto
(fun hello world -> hello .append world) "Hello" "World"
  .isFunction

```

Shouldn’t applying all the arguments result in the string value “HelloWorld” instead of returning another function?

---

<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:** [September 21, 2021, 4:02am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/13 "2021-09-21T04:02:28Z")

</div>

You’re right, this is a bug. i need to look into it

---

<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:** [September 21, 2021, 6:27pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/14 "2021-09-21T18:27:56Z")

</div>

Just released Version 1.0.2 with the fix inside.

---

<div class="post-metadata">

**Author:** ![gutierlf](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gutierlf/32/4275_2.png) [@gutierlf](https://discourse.elm-lang.org/u/gutierlf)\
**Post date:** [September 21, 2021, 11:13pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/15 "2021-09-21T23:13:14Z")

</div>

Really cool! Like others, I’m liking the docs. One comment there: I’ve only browsed them from my phone so far, and I kept wanting a “next page” link at the bottom of each page, like the Elm guide. The only navigation option I found so far was the menu, and that takes a few more gestures than a “next page” link would.

---

<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:** [September 22, 2021, 3:56am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/16 "2021-09-22T03:56:46Z")

</div>

I like the idea. This should be added to elm-book @georgesboris.

---

<div class="post-metadata">

**Author:** ![georgesboris](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/georgesboris/32/3598_2.png) [@georgesboris](https://discourse.elm-lang.org/u/georgesboris)\
**Post date:** [September 22, 2021, 8:41am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/17 "2021-09-22T08:41:43Z")

</div>

@gutierlf @Lucas_Payr – yup, already on the pipeline 🙂 thanks folks!

---

<div class="post-metadata">

**Author:** ![tgelu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/tgelu/32/1008_2.png) [@tgelu](https://discourse.elm-lang.org/u/tgelu)\
**Post date:** [September 22, 2021, 11:05am UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/18 "2021-09-22T11:05:27Z")

</div>

I also think a “why” section in the docs would help those like me, unfamiliar with the need for scripting languages, understand the problem Leaf solves 😃

---

<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:** [September 23, 2021, 5:53pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/19 "2021-09-23T17:53:39Z")

</div>

The docs now includes a “when to use this package” [chapter](https://orasund.github.io/leaf-lang/#/basics/when-to-use-this-package).

---

<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:** [October 3, 2021, 5:54pm UTC](https://discourse.elm-lang.org/t/leaf-a-scripting-language-for-elm/7727/20 "2021-10-03T17:54:31Z")

</div>

Closed on October 3, 2021, 5:54pm UTC

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