Created
January 22, 2014 16:01
-
-
Save timyates/8561373 to your computer and use it in GitHub Desktop.
Idiosyncrasies of Groovy's with method and null values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.groovy.runtime.NullObject | |
def map = [ a: 1, b: 2 ] | |
// map.c is equal to null | |
assert map.c == null | |
// But then if we use with | |
map.c.with { c -> | |
// c is not equal to null! | |
assert c != null | |
// c is an instance of NullObject | |
assert c instanceof NullObject | |
// c is also equal to the singleton instance of NullObject | |
assert c == NullObject.INSTANCE | |
// But this is a MUCH neater way of checking | |
assert c.is( null ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assert c != null
sounds like a bug to me. Eithernull
should always be aNullObject
(which sounds like it's in the same realm as Scala'sOptional
), or it should be actuallynull
. And for any cases where it cannot be one or the other, it should behave as if it is whichever one is "standard". (ie.NullObject == null
)