Feature/empty values are nil#3
Feature/empty values are nil#3gavinlaking wants to merge 2 commits intorsslldnphy:masterfrom gavinlaking:feature/empty_values_are_nil
Conversation
|
For posterity, here are the reasons I don't agree with this pull request. Sorry Gav! Making Option[[]], Option[{}] and Option[""] return NoneLooked at a certain way, it does make a kind of sense. I can see how you got the idea. However, I think there are some important things we need to consider. The first one is - why would you be creating an optional collection - what value would you be getting from doing so? You might want to only do something if there are elements in the collection. You could conceivably want to do just one thing if the collection has at least one elements, This is easily and more readably (I would argue) achieved just using a conditional - even though I hate conditionals. or So there is nothing you can do with a None that you can't already do with an empty collection. What about the way it is - with a difference between a Some[empty_collection] and a None? One case I can think of is allowing you to distinguish between information you don't know, So for example, suppose you have a Customer model, and there's a part of your system that works out Calculating the vouchers is an expensive process so you only do it when you need to, You decide to represent the set of vouchers in the hash as an Option. But some sets of customers are not eligible for any vouchers. So you want to store this in the hash. But if there's a difference between a None and a Some[empty_collection], you can distinguish between That's why I say there's a difference between an empty bucket and the absence of a bucket. You can fill an empty bucket with sand - but you can't fill the absence of a bucket with anything. Another issue is that if you make Option[empty_collection] evaluate to None, you cannot get this distinction back. However, if Option[[]] => Some[[]], but you happen to need Option[empty_collection] to evaluate to a None, Having said all this, I'm really pleased that you're interested enough in Options to be submitting a pull request! I do have a suggestion that you could look at though, if you're interested? Anyway, I thought it might be nice if there was a companion gem to optional that implemented the Either type in Ruby. |
Discuss…