DEV Community

Discussion on: #30daysofelm Day 1: Show range slider value in p tag

Collapse
 
bukkfrig profile image
bukkfrig

Whenever a type has multiple terms, it means its a generic type.

If you're familiar with Java style generics, then List Int in Elm would be Java's List<Integer>. The purpose of generics is so that we can define what a List is once, but on different instances of it the operations (like push and get) have their type signatures updated so that they accept or return different things, like Int values in our case. We didn't have to define a IntegerList and a StringList and a FooList and so on, just List a in Elm or List<T> in Java.

So similarly, Html Msg would be like Html<Msg> in Java. This is some data structure for holding HTML nodes, where some of it's operations (related to event listeners in this case) can produce values that have the type Msg. (Msg is not a special type, you defined it yourself with type Msg = Change String!).

And still similarly, Program is a generic type with 3 type parameters, so Program Flags Model Msg in Elm is like Program<Flags, Model, Msg> in Java. It's a Program that uses some type called Flags in some of it's operations that deal with program startup, some type called Model in some other operations that deal with state, and some type called Msg in other operations that deal with messaging. (Again Model and Msg are simply defined by you!)

All that's left to explain is (), which is also less special than it might first appear. It's just a type that can only have one value - () is the name of the type, and of the value. It just gets called the "unit" type, and the value is actually an empty Tuple. So operations on a Program () Model Msg can only ever get one value, the empty Tuple (), in its operations that deal with starting up - and so a Program generated by Browser.sandbox always starts up the same.

So all of this is a bit like public static void main(String[] args) in Java. As a beginner, you just write it because somebody told you to and the language demands it. Eventually you learn about when you should make things public and when static and when should they return void and how to use String[] arrays, and then you look at the boilerplate code and understand that those things really do (have to) apply to main. Same with Elm's boilerplate eventually, but don't worry too much about it as a beginner.