Title Text
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11482776/_3686c1c2-caf0-46db-aacd-d9c65bf30e4b.jpg)
Algebraic data types
data what ?
data
types
Algebraic
What are types used for?
Optimisation !
Fewer bugs
- No !
- but yes...
- but no...
![](https://media1.giphy.com/media/TjMk9QVA1O122lohHJ/giphy.gif)
Complex
- No !
- it's just a shift in complexity towards the build part
Types can simplify your code !!!
![](https://media0.giphy.com/media/zyocE1cSCWWs0/giphy.gif)
Accidental complexity
Optionality
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11482886/_b8539513-6b01-4caf-9d71-d51bc9bd81fb.jpg)
Null pointer
let person = {
name: "Alice",
age: 30,
address: null
};
Null pointer
interface Person {
id: number;
name: string;
age: number;
}
function findPersonById(people: Person[], id: number): Person | undefined {
for (const person of people) {
if (person.id === id) {
return person;
}
}
return undefined;
}
Charles Antony Richard Hoare
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484181/Sir_Tony_Hoare_IMG_5125.jpg)
Option type
Option type
type option('value) =
| None
| Some('value);
Schrodinger's cat experiment
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484208/Pasted_image_20240719150720.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484205/Pasted_image_20240719150628.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484206/Pasted_image_20240719150813.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484207/Pasted_image_20240719150906.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484221/Capture_d_e%CC%81cran_2024-07-21_a%CC%80_00.00.18.png)
Type option in action
Type option in action
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484224/pasted-from-clipboard.png)
Success
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11482911/_83e2ab07-d1b5-43c7-a07b-03936bb88754.jpg)
function divide(a: number, b: number): number
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484226/pasted-from-clipboard.png)
Signatures can lie !
![](https://media3.giphy.com/media/3o85gdhlpxVz8TjsTC/giphy.gif)
Type result
type result('a, 'e) =
| Ok('a)
| Error('e);
Type result in action
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11484975/pasted-from-clipboard.png)
Always favor the result type as much as possible over exceptions
please make your function signatures explicit
let divide = (a: number, b: number): result(string, int);
let divide_exc = (a: number, b: number): number;
@type result :: {:ok, integer()} | {:error, string()}
@spec divide(integer(), integer()) :: result
def divide(a, b) do
try do
result = divide!(a, b)
{:ok, result}
rescue
e in RuntimeError -> {:error, e}
end
end
def divide!(a, b) do
# make the division
end
Definition
Sum type
Sum type
type animal = "cat" | "dog" | "cow";
Sum type
type option(int) =
| None
| Some(int);
Product type
Product type
type dice_value = 1 | 2 | 3 | 4 | 5 | 6;
type player = "Alice" | "Bob"
type roll_of_dice = {
player: player,
value: dice_value
}
roll_of_dice = dice_value * player
An algebraic type is a sum type of product type
An algebraic is a potential recursive sum type of product type
type intList =
| Empty
| Node({
value: int,
next: intList,
});
Avoid avoidable complexity
Accidental complexity
type state = {
isLoading: bool,
error: error | null,
data: data | null
}
![](https://media1.giphy.com/media/v3LxMGs7TObBO5Uqik/giphy.gif)
Accidental complexity
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11485042/pasted-from-clipboard.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/86447/images/11485047/pasted-from-clipboard.png)
![](https://media2.giphy.com/media/LPXDoKp2aHrKLmvHyo/giphy.gif)
clap now!
![](https://media0.giphy.com/media/NmQpsg9cAkr1ntt8uU/giphy.gif)
deck
By Romain Commandé
deck
- 24