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

SPEL: Introduce a new null-safe access operator for properties [SPR-5664] #10335

Closed
spring-projects-issues opened this issue Apr 8, 2009 · 3 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 8, 2009

Oliver Becker opened SPR-5664 and commented

SPEL doesn't have a way to specify whether property accessors should be null safe or not.
Currently they are not null safe, i.e. "foo.bar" will throw an exception if foo is null. (However, there's an inconsistency here, see #10334)

It would be great if SPEL could introduce an additional operator '?.' (besides the '.' operator) for null-safe access.
Thus "foo.bar" would still throw an exception, but "foo?.bar" would evaluate to null resp. the default value of bar (for primitive types).

There's an interesting blog post about this idea at http://codervirtue.blogspot.com/2009/03/null-safe-invocation-in-java-7.html


Affects: 3.0 M2

@spring-projects-issues
Copy link
Collaborator Author

Andy Clement commented

I actually prefer the groovy style ( http://groovy.codehaus.org/Operators ) where ?. is the null safe navigation operator.

Expression "a.b.c" then throwing an exception if a or b is null, but "a?.b?.c" just evaluating to null if either a or b are null. That blog page looks a bit more complicated than I'd like to tackle (default values, etc...)

Would the simple groovy meaning be suitable for you or do you really need more power than that?

@spring-projects-issues
Copy link
Collaborator Author

Oliver Becker commented

The simpler groovy semantics is fully sufficient at the moment (at least for us). Thanks, Andy.

@spring-projects-issues
Copy link
Collaborator Author

Andy Clement commented

Ok - committed. Support for both null safe property navigation ?. and (as a bonus) the elvis operator ?: are now in. Let me know if you have problems with them.

Elvis is for this case, where you normally write:

user.id!=null?user.id:"unknown user"

you can use elvis

user.id?:"unknown user"

So if the first expression evaluates to non null it is the result, if it is null then the second expression is the result.

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

No branches or pull requests

2 participants