4 ms·
Ruby's & operator. At first I hated it because of how incomprehensible it was to someone coming from seemingly every other language, but I've started to use it
by laogai 8y ago
Ruby's & operator.
At first I hated it because of how incomprehensible it was to someone coming from seemingly every other language, but I've started to use it more and more and wishing it existed in other languages.
It's especially useful for when the variable access is many characters. e.g.
`if hackerNews.frontPage.votingCircleExists:`
with nil checks is
`if hackerNews && hackerNews.frontPage && hackerNews.frontPage.votingCircleExists`
and with & operator is:
`if hackerNews&.frontPage&.votingCircleExists`.
- kjeetgill 8y agoThis is exactly how Python's 'and' and 'or' work too. The first time I saw it I was baffled. It took me a good while to fully wrap my head around it. I grew to love it. Edit: I was mistaken, it's a little different. This is more like groovys safe navigation ?.
- deleted 8y ago[deleted]
- christophilus 8y agoAgreed. Null conditional operators are definitely handy in languages that have nulls. C# has this in the form of ?.
- kjeetgill 8y agoThis is there piece of C# I'm most envious of missing out on in Java.