Elm is a...
...language
"there is a formal proof that a well-typed ML program does not cause runtime type errors" -Robin Milner -Wikipedia
https://en.wikipedia.org/wiki/ML_(programming_language)
Usability:
"the time it takes to get [from] a novice level to actually a product that you can show your friends and be like, “Hey, check this out.”"
Helpful errors:
"[let's] change the relationship people have with the compiler from adversary to assistant”"
Gradual Learning:
"You can design the language such that as someone gets started and gets productive, slowly they realize these concepts in a way that builds upon each other in a way that works for people"
http://elm-lang.org/blog/compiler-errors-for-humans
Update:
Given the model and some message (event), how do we generate the new state?
Model:
The (only) place where the state of your application is tracked and stored
View:
Given the model, how do I render the relevant HTML DOM code? (Or PDF, SVG, etc)
Commands:
Tell the runtime to do something with side effects! (Generate random numbers, HTTP requests, access local storage, etc)
Subscriptions:
Listen for external events (keyboard, mouse, websockets, browser, etc)
But wait, how will anything ever happen?
There's no NULL, just Maybe
> type Maybe a = Nothing | Just a
> Nothing
Nothing : Maybe a
> Just
<function> : a -> Maybe a
> Just "hello"
Just "hello" : Maybe String
> Just 1.618
Just 1.618 : Maybe Float
There's no NULL, just Maybe
type alias User =
{ name : String
, age : Maybe Int
}
canBuyAlcohol : User -> Bool
canBuyAlcohol user =
case user.age of
Nothing ->
False
Just age ->
age >= 21
https://guide.elm-lang.org/error_handling/maybe.html
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
main =
Html.beginnerProgram
{ model = model
, view = view
, update = update
}
-- MODEL
type alias Model = Int
model : Model
model =
0
-- UPDATE
type Msg
= Increment
| Decrement
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
-- VIEW
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick Increment ] [ text "+" ]
]