Juan Domínguez González opened SPR-16929 and commented

When parsing an expression which combines null-safe and list/array navigation it fails with a parse exception.

Example: accessing first element of a (possibly null) list/array of strings: stringList?[0]

org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'select(?[)

I'm not sure if the syntax I've used is correct, but, to me it seems the most natural one.

Combining the null safe operator and navigation using get, seems to work: e.j: stringList?.get(0)

Alternative syntaxes, such as (stringList?)[0] or stringList?.[0], also fail to compile..


Affects: 4.3.16

Comment From: sbrannen

@aclement, ping.

Comment From: renjfk

Well, if it's only matter of accessing null-safe first/last element listOrArray?.^[true] and listOrArray?.$[true] should do the trick as workaround.

Comment From: john-liu

I also want this feature, to support a safe index navigation.

Just like groovy did in 3.0: use ?[] to support safe index navigation. https://groovy-lang.org/operators.html#_safe_index_operator

Comment From: Trympyrym

This syntax clashes with collection selection in case of collection is context root.

Its hard to decide if ?[expr] (not collection.?[expr]) is selection by boolean expr or null-safe indexer by integer expr. Its possible when expr is boolean or int literal, but in case of ?[callSmth()] i have no idea how to implement this without great rework

Comment From: Trympyrym

However implementing syntax like stringList?.[0] seems much easier, reusing SAFE_NAVI token

Comment From: sbrannen

  • superseded by #29847