|
|
|
|
|
by simen
2888 days ago
|
|
> For me using an exception to indicate the lack of a value corresponding to a key in a map (is it really exceptional for this to happen?) Sometimes it is, sometimes it isn't. Most languages allow you to choose whether you want a hash to throw a key error on non-existing keys or return a signal value. My favorite is the uber-flexible approach and nice syntax in Crystal: hash[foo] is sugar for hash.fetch(foo)
hash[foo]? is sugar for hash.fetch(foo, nil)
hash.fetch(foo) will return the default value of the hash table on nonexistent keys if one was assigned, otherwise raise keyerror
hash.fetch(foo, nil) will return nil on nonexistent keys
|
|