I read the first example of Lily on the site. I found nothing offensive in the language at all - in fact there's a lot to like particularly the readability.
Side rant. Unfortunate I can't get past the decision to use exception handling to deal with non-existing keys in map. I'm of the exception-disliking school which is thankfully growing but I guess not yet universal. 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?) is a textbook example of exception-abuse where the code is obfuscated by the non-locality of the control flow.
I actually didn't realize it was doable (not a heavy rust user - just an enthusiast). Kinda hidden away in docs under an example [0] and isn't in the book under hash maps [1]. Might be worth calling out somewhere explicitly? People getting started with rust probably aren't familiar with the trait-based operator overloading system yet.
> 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
You can use dict's get() method to get back a None for missing keys instead of throwing a KeyError exception. That avoids the overhead of checking `if key in dict` or catching KeyError.
Another option, it's very easy to wrap a dict in a defaultdict:
class optionaldict(defaultdict):
"""
A defaultdict that disregards KeyErrors and returns None for missing keys.
"""
def __init__(self, *original, **kwargs):
super().__init__(lambda: None, *original, **kwargs)
Side rant. Unfortunate I can't get past the decision to use exception handling to deal with non-existing keys in map. I'm of the exception-disliking school which is thankfully growing but I guess not yet universal. 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?) is a textbook example of exception-abuse where the code is obfuscated by the non-locality of the control flow.