kaffeine icon indicating copy to clipboard operation
kaffeine copied to clipboard

Add Existential Operator

Open akidee opened this issue 14 years ago • 2 comments

In CS, this a feature I heavily use when writing templates, and it is very useful, since it makes your code more safe (because null/undefined will throw an error on accessing properties)

Example:

zip = lottery.drawWinner?().address?.zipcode

akidee avatar Apr 17 '11 01:04 akidee

I did once upon a time have one (actually I invented the one in CoffeeScript :)

I should be able to bring it back as I agree, it was useful. I decided to make it a little simple here though : more like a non-strict comparison with null, so your example would work like :

zip = (_ex = lottery.drawWinner()) != null && _ex.address && _ex.adress.zipcode

weepy avatar Apr 17 '11 08:04 weepy

I've put up a gist to explore the possibilities here :

https://gist.github.com/923899

weepy avatar Apr 17 '11 10:04 weepy