evancz/elm-architecture-tutorial

Example 4 uses unfamiliar syntax to create Counter.Context

Josh211ua opened this issue · 3 comments

In example 4, the CounterList creates a Counter.Context record using the syntax:

let context =
      Counter.Context
        (Signal.forwardTo address (Modify id))
        (Signal.forwardTo address (always (Remove id)))

As someone who is new to elm this syntax left me somewhat confused. Counter.Context is a type alias and it was not obvious to me that a type alias could be used to construct a record of that type. I have since confirmed that this is the case, but was unable to find documentation about this.

The example would have been slightly simpler for me to follow if it had avoided or explained this new syntax. It seems easy to avoid by changing the example to:

let context =
      {
        actions = (Signal.forwardTo address (Modify id))
        remove = (Signal.forwardTo address (always (Remove id)))
      }

And I would like to add a thing about that.

type alias Context =
    { actions : Signal.Address Action
    , remove : Signal.Address ()
    }

When we use the syntax:

let context =
      Counter.Context
        (Signal.forwardTo address (Modify id))
        (Signal.forwardTo address (always (Remove id)))

I assume the actions are not infered from the order but from the signature (hence the always).
Am I right?

Nope, order appears to matter:

this works:

> type alias Context = {int: Int, string: String}

> test = Context 3 "correct"
{ $int = 3, string = "correct" } : Repl.Context
> test.int
3 : Int
> test.string
"correct" : String

this does not work


> Context "incorrect" 3
-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm

The 1st argument to function `Context` is causing a mismatch.

4│   Context "incorrect" 3
             ^^^^^^^^^^^
Function `Context` is expecting the 1st argument to be:

    Int

But it is:

    String


>

This is not used in http://guide.elm-lang.org/ anymore