What does that mean?
- For a given input, you always get the same output
- The output cannot depend on any external factors (e.g. File on disk, some external variable)
- Functions cannot do things, only return data
- Different from imperative model
- Command/query separation
Following these ideas leads to further ideas:
- No globals, local state only
- Immutable data
- Read only
- Duplicates when modifying
Languages such as Ruby and Javascript have duck typing
- Pass anything as an argument to a function, the language doesn’t care
- Will try to call the methods or fail at runtime
- Javascript will auto-convert, did you want a string?
Elm has static types
You may have found types constraining in the past, waste of time
Elm’s compiler works with you
- Types are optional with type inference
- Compiler detects inconsistent code, don’t rely on tests/coworkers
- Compiler detects missing conditions
- Compiler has some of the friendliest error messages
Best of all
- No stack trace
- No runtime errors!
Not just type safety but also better modeling
How do you model in a dynamic language?
- Three state boolean with null? (antipattern)
- String? What if other string is passed in?
Easy to read and understand Elm compiler ensures only one of these three values Elm compiler checks case incomplete case statements
Compiles to javascript and:
- Possibly CSS
- Possibly HTML
- Possibly Canvas
Javascript targeted for now, plans to support web assembly
User-friendlyness is a core value
- Underpinned by some awesome CS and functional theory
- Try to be user accessible
- Avoid “scary” jargon like “Monad”
- Great compiler messages
- Keeps simplifying language
- Avoids custom operators
Anything can be nil/null in dynamic languages
- Need to check all the time
- In practice we don’t
- Runtime errors
In elm we use Maybe to explicitly tag values that are optional
- Compiler forces you to check for nothing case
- Values not wrapped in maybe are guaranteed to be safe
- No runtime errors!
- Functional restrictions
- All actions can be returned as “command objects” that elm will execute at a later time
- Handled by the runtime
- Inherently not pure
- Random method returns command objects
- Asynchronous
- Triggers event you can listen to with result
- HTTP is inherently impure
- Various HTTP methods return command objects
- Asyncronous
- Triggers event you can listen to
- Current time is inherently non pure
- We don’t want a command here
- Subscriptions
- Functions deal with pure values
- Inherently non pure
- Sending messages is a command
- Receiving responses is a subscription
Defining types
- Convert a model to markup
- Trigger Msg based on DOM events
- “Steps” model forward in response to a Msg
- FRP
- Like React + Redux
- Time traveling debugger
- This pattern is infinitely composable
- Parents delegate to children
- Children return to their parents
- Single data source at top
Used to be a bit convoluted