Skip to content
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

Closed

Conversation

mmanashirov
Copy link

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.

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.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 11, 2023
@sbrannen sbrannen marked this pull request as draft April 11, 2023 20:56
@sbrannen sbrannen added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) labels Apr 11, 2023
Comment on lines +1778 to +1782
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].

Copy link
Member

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).

@simonbasle
Copy link
Contributor

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 👍

@simonbasle simonbasle added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 18, 2023
@mmanashirov
Copy link
Author

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.

@simonbasle
Copy link
Contributor

simonbasle commented Apr 18, 2023

@simonbasle simonbasle closed this Apr 18, 2023
@sbrannen
Copy link
Member

Thanks for pointing out that this behavior is specific to Strings.

You're welcome.

After experiencing a slight burn as a result of this behavior on Strings,

I can totally see how you got burned by that: it was an undocumented "feature".

I made the false assumption that it consistently behaves according to Groovy's rules for other types as well.

Well, you know what they say about assumptions... <fill in the blank>

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. 😇

@mmanashirov
Copy link
Author

We must have had the same 10th grade history teacher. 😂

simonbasle added a commit to simonbasle/spring-framework that referenced this pull request May 5, 2023
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
simonbasle added a commit that referenced this pull request May 5, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: documentation A documentation task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants