-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update documentation to clarify Elvis operator semantics #30318
Conversation
Updated ternary expression that could be misleading for those unfamiliar with Groovy's Elvis operator semantics. Also added a caution snippet to distinguish the varying interpretations and reinforce the expected behavior in SpEL.
CAUTION: Beware that the behavior of the Elvis operator follows boolean semantics as opposed | ||
to object reference semantics. Thus, rather than returning the first operand if it is non-null, | ||
it returns the first operand if it evaluates to true based on the rules that comprise | ||
https://groovy-lang.org/semantics.html#the-groovy-truth[The Groovy Truth]. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not entirely true.
SpEL does not honor all of Groovy's "truth" rules.
The SpEL Elvis operator checks for values that are non-null and non-empty (for Strings).
Thanks @mmanashirov for pointing that subtlety. As @sbrannen has pointed out, the wording in the change is not entirely accurate but the concern is valid. I'll treat it more as a ticket than a PR and will come up with a satisfactory wording in a separate commit, closing this as superseded. No more action required on your part 👍 |
Thanks for pointing out that this behavior is specific to Strings. After experiencing a slight burn as a result of this behavior on Strings, I made the false assumption that it consistently behaves according to Groovy's rules for other types as well. You just saved me from another burn in the future! Thank you. |
You're welcome.
I can totally see how you got burned by that: it was an undocumented "feature".
Well, you know what they say about assumptions... I still recall what my 10th grade history teacher told my class about assumptions. Though I don't think it's appropriate to quote that here. 😇 |
We must have had the same 10th grade history teacher. 😂 |
This commit improves both the javadoc and the reference guide section on the Elvis SpEL operator to clarify that in addition to `null` objects, empty Strings also lead the operator to evaluate to its second operand. The reference guide's advanced snippet is modified to use such an empty String instead of `null` to make that behavior prominent with some code. See spring-projectsgh-30318 Closes spring-projectsgh-30352
This commit improves both the javadoc and the reference guide section on the Elvis SpEL operator to clarify that in addition to `null` objects, empty Strings also lead the operator to evaluate to its second operand. The reference guide's advanced snippet is modified to use such an empty String instead of `null` to make that behavior prominent with some code. See gh-30318 Closes gh-30352
Updated ternary expression that could be misleading for those unfamiliar with Groovy's Elvis operator semantics.
Also added a caution snippet to distinguish the varying interpretations and reinforce the expected behavior in SpEL.