Reputation: 969
In the Elm language, I'm having a hard time explaining my question... In these snippets in elm: I understand the signature in something like
update : Msg -> Model -> Model
where the parameters / output is separated by arrows, but how do I read / grok things like:
Sub Msg
Program Never Model Msg
In:
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
Upvotes: 8
Views: 642
Reputation: 36375
In a type signature, parameter types are separated by ->
, with the last type being the return value.
If there are no ->
symbols, then it means it is a value of that type. In your main
example, the type of main
is Program Never Model Msg
. It has no arrows, so it takes no parameters.
Now, each parameter and the return value in the type annotation may have several things separated by spaces, as in your main
example. The leftmost is the type, followed by type parameters separated by spaces.
Program Never Model Msg
| | | |
| ------|-----
type type parameters
A type parameter is similar to Generics in a language like C#. The equivalent syntax in C# would be:
void Program<Never, Model, Msg>()
C# doesn't directly correlate because it has a different way of constraining generic type parameters, but the general idea holds.
The Elm guide doesn't currently have a great deal of info, but here is the section talking about types.
Upvotes: 14
Reputation: 2923
Sub Msg
, List Int
, Program Never Model Msg
Sub
, List
and Program
are type constructors. You can think about them as functions that take a type and return another type.
By themselves, Sub
, List
and Program
are not complete type. They are like a puzzle missing a piece. When one adds the missing piece, the puzzle is complete.
I usually read them in my head using the word of
as in a List
of Int
s, a Program
of Never
, Model
and Msg
.
Upvotes: 6