You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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?
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.
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
The text was updated successfully, but these errors were encountered: