Skip to content

Instantly share code, notes, and snippets.

@xlson
Created June 18, 2010 09:21
Show Gist options
  • Save xlson/443431 to your computer and use it in GitHub Desktop.
Save xlson/443431 to your computer and use it in GitHub Desktop.
Groovy custom truth and equals()
class Predicate {
boolean value
boolean asBoolean() { value }
}
// Works:
assert new Predicate(value: true)
assert !new Predicate(value: false)
assert (new Predicate(value: true) as Boolean == true)
assert (new Predicate(value: false) as Boolean == false)
// Does not work:
assert (new Predicate(value: true) == true)
assert (new Predicate(value: false) == false)
//Output:
//Assertion failed:
//
//assert (new Predicate(value: true) == true)
// | |
// Predicate@3ee3f8b9 false
//
// at ConsoleScript29.run(ConsoleScript29:13)
@andresteingress
Copy link

the following call coerces to a boolean value:

assert new Predicate(value: true)

== causes the equals method to jump in, no boolean coercion here

@xlson
Copy link
Author

xlson commented Jun 18, 2010

Thanks for the explanation, that makes sense.

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