4 ms·
This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from
by neillyons 7y ago
This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg.
# python
names = []
names[0] # <- raises IndexError
In Elm you are forced to always consider this possibility.
# Elm
names = []
case List.head names of
Just name ->
name
Nothing ->
"empty"
- MawKKe 7y agoIdiomatic python: v = names[0] if names else "empty"
- lgas 7y agoYes, but the point is you can forget the check in python but not in Elm.
- dorfsmay 7y agoThis always felt weird to me given that Python does a good job with dict: d = {} assert(d.get("blah"), None) I was impressed by Rust the first time I run into: let mut last; for i in &[1, 2, 3] { last = i; } println!("{}", last); >> borrow of possibly uninitialized variable: `last`