Re:Lily: An interpreted language with a focus on expressiven...
by staticassertion on Thursday July 19, 2018 @09:56PM
(#17571885)
Rust makes a similar decision, despite having Option and Result.
If you want the Option behavior, there's .get()
Anyways, very cool looking language.
Re:Lily: An interpreted language with a focus on expressiven...
by Ar-Curunir on Friday July 20, 2018 @12:10AM
(#17572391)
But from what I've seen, most idiomatic Rust code doesn't use the `map[key]` syntax, but instead uses `map.get(key)`.
Re:Lily: An interpreted language with a focus on expressiven...
by steveklabnik on Friday July 20, 2018 @05:08AM
(#17573518)
We must look at very different code :)
Re:Lily: An interpreted language with a focus on expressiven...
by bpicolo on Friday July 20, 2018 @11:11AM
(#17575614)
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.
[0] https://doc.rust-lang.org/std/collections/struct.HashMap.htm... [1] https://doc.rust-lang.org/book/second-edition/ch08-03-hash-m...
Re:Lily: An interpreted language with a focus on expressiven...
by steveklabnik on Friday July 20, 2018 @03:04PM
(#17577898)
Yeah, might be worth it on the API docs, for sure. Maybe file a bug? :)
Re:Lily: An interpreted language with a focus on expressiven...
by simen on Friday July 20, 2018 @03:27AM
(#17573113)
> 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
Re:Lily: An interpreted language with a focus on expressiven...
by ummonk on Thursday July 19, 2018 @09:02PM
(#17571686)
Seems particularly weird given that it has "Algebraic data types (with Option and Result predefined)."
Re:Lily: An interpreted language with a focus on expressiven...
by enitihas on Thursday July 19, 2018 @04:14PM
(#17569841)
Well, for good or for bad, python does the same.
Re:Lily: An interpreted language with a focus on expressiven...
by cpeterso on Thursday July 19, 2018 @04:36PM
(#17570024)
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.
Re:Lily: An interpreted language with a focus on expressiven...
by wool_gather on Thursday July 19, 2018 @05:02PM
(#17570229)
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)
Re:Lily: An interpreted language with a focus on expressiven...
by Quelklef on Friday July 20, 2018 @12:53AM
(#17572571)
This is almost always a bad idea, though; it doesn't distinguish between
{k: None}[k]
and {}[k]
Re:Lily: An interpreted language with a focus on expressiven...
by bvrmn on Friday July 20, 2018 @01:06AM
(#17572623)
It's not a generic option, you are polluting memory with non-existing keys.
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.