Re:The Groovy project is looking for a new home
by bjfish on Tuesday January 20, 2015 @08:52AM
(#8916893)
Yes, I see || as a wart or crufty. Think about the type signature of || in ruby:
<Type 1 or Type2> ||(Type1 arg1, Type2 arg2)
but in how many other languages is this? boolean ||(arg1, arg2)
C, Java, Objective-C, C++, C#, PHP vs Ruby, Perl, JavascriptIf || is used more frequently as a logical operator returning true(1) or false(0) to test logical or (not to be used for assignment), why overload this operator?
Having a separate operator like ?: better shows the intention of the usage. It also resembles the ternary function which has similar functionality.
Re:The Groovy project is looking for a new home
by nimblegorilla on Monday January 19, 2015 @09:48PM
(#8915216)
It's subtle, but I think the difference is that in ruby when potentially_falsy_value is false you will get the safeDefault. But with ?: you would get the potentiallyFalsyValue as false. The only time you get safeDefault is when potentiallyFalsyValue is nil/null
Re:The Groovy project is looking for a new home
by prophetjohn on Wednesday January 21, 2015 @08:19PM
(#8926932)
The docs seem to state otherwise
One instance of where this is handy is for
returning a 'sensible default' value if an
expression resolves to false or null
http://groovy.codehaus.org/Operators#Operators-ElvisOperator(?:)
Based on the docs for elvis, it looks like
is exactly equivalent to in RubyFurther, the null-safe operator seems to be the same as #try in Rails and overuse of either is probably a bit of a smell that you might be violating Tell Don't Ask