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

Tricky Solution for 'test05_ElvisAndSafeNavigation' #26

Open
abelsromero opened this issue Aug 8, 2014 · 2 comments
Open

Tricky Solution for 'test05_ElvisAndSafeNavigation' #26

abelsromero opened this issue Aug 8, 2014 · 2 comments

Comments

@abelsromero
Copy link

I don't consider myself a Groovy expert but not a novice either and I found quite a tricky behauvior that may need some extra clarification in test05.
I wrote the following solution, which may seem correct:
message = 'Hello ' + userService.getLoggedInUser()?.getFirstName()?:'Anonymous' + '!'
However, unless parenthesis are added as follows it does not work
message = 'Hello ' + (userService.getLoggedInUser()?.getFirstName()?:'Anonymous') + '!'

Seeing why in one case it returns "Hello null" is easy, but understanding why on the other the result is "Hello Ronaldo" is not.

Seeing the missleading behauviour of elvis operator, ¿wouln'd it be nice to add a test for it, or some extra information?

Thanks

@nadavc
Copy link
Owner

nadavc commented Aug 8, 2014

Care to submit a pull request? :)
On Aug 8, 2014 9:06 AM, "abelsromero" [email protected] wrote:

I don't consider myself a Groovy expert but not a novice either and I
found quite a tricky behauvior that may need some extra clarification in
test05.
I wrote the following solution, which may seem correct:
message = 'Hello ' +
userService.getLoggedInUser()?.getFirstName()?:'Anonymous' + '!'
However, unless parenthesis are added as follows it does not work
message = 'Hello ' +
(userService.getLoggedInUser()?.getFirstName()?:'Anonymous') + '!'

Seeing why in one case it returns "Hello null" is easy, but understanding
why on the other the result is "Hello Ronaldo" is not.

Seeing the missleading behauviour of elvis operator, ¿wouln'd it be nice
to add a test for it, or some extra information?

Thanks


Reply to this email directly or view it on GitHub
#26.

@EPadronU
Copy link

EPadronU commented Mar 3, 2016

This is a very old issue but in order to avoid further confusion for a future reader: the problem is not a "misleading behauviour of the elvis operator". The need of parenthesis shows that the cause of such problem is the simple and old operators' precedence rule. + has a higher priority than ?: so, without parenthesis the expression is evaluated as follows: ( 'Hello ' + userService.getLoggedInUser()?.getFirstName() ) ?: ( 'Anonymous' + '!' ).

Update: IMHO there's no need for an extra test or anything similar because is a matter of CS 101 and not a language quirk. Nonetheless, a comment mentioning that + has a higher priority than ?: could be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants