Skip to content

Instantly share code, notes, and snippets.

@timyates
Created January 22, 2014 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timyates/8561373 to your computer and use it in GitHub Desktop.
Save timyates/8561373 to your computer and use it in GitHub Desktop.
Idiosyncrasies of Groovy's with method and null values
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 )
}
@cdeszaq
Copy link

cdeszaq commented Jan 27, 2014

assert c != null sounds like a bug to me. Either null should always be a NullObject (which sounds like it's in the same realm as Scala's Optional), or it should be actually null. 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment