Created
June 18, 2010 09:21
-
-
Save xlson/443431 to your computer and use it in GitHub Desktop.
Groovy custom truth and equals()
This file contains hidden or 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
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) |
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
the following call coerces to a boolean value:
assert new Predicate(value: true)
== causes the equals method to jump in, no boolean coercion here