"Functional Programming is writing programs with (pure) functions and (immutable) data"
~ Probably someone from the internet
Focus of this talk
ADTs are product types, sum types and compositions of those
Often also called records or (named) tuples
Combine values with "AND"
Often also called coproduct or disjoint unions
Combine values with "OR"
*
* Scala's way of writing ADTs is truly the worst of any FP language -_-'
Mix and nest as you wish
1. "Exhaustiveness Checking"
Compiler is helping us out:
=> Incredibly helpful for easy and safe refactorings/changes
2. Help with partiality when modelling application state
Can have many inconsistent states!
Using an ADT the compiler will force you to handle partiality/errors:
2.1. Limited amount of possible states
Possible states: 2 * 2 * 2
Possible states: 1 + 1 + 1 + (2 * 2)
=> This shows where sum and product in the names come from
=> Great for finite state machines
3. Expressive data types are a design tool and documentation
Can you guess what program this is by just looking at the data model for it?
4. Keeping data separate from implementation details helps with reusability of your domain model
This brings us to...
A lot of things we do with data involves (de)serialization. Writing the code for this by hand is tedious and error-prone.
Typeclasses help to do this without reflection, but normally require to still write the logic for each new type
Serializing to json with circe:
Nobody has time for this...
Circe can derive the typeclass instances for sum and product types:
This is still type-safe and fast (no runtime reflection) and can reduce the boilerplate to zero (for automatic derivation)!
If you want the same for your own code you have to either:
Magnolia example:
+
Magnolia has decent error messages if a type has no typeclass instance
E.g. No typeclass instance for "LocalDate" type
"Making Impossible States Impossible" ~ Richard Feldman
"Purescript: Tomorrow's JavaScript Today" ~ Kris Jenkins
"How Elm Slays a UI Antipattern" ~ Kris Jenkins