Skip to content

Add safe navigation ?. and null coalescing ?? operators #536

@drosenbauer

Description

@drosenbauer

I propose that Beanshell implement a safe navigation operator, like many other languages. This would be a deviation from Java syntax, but Beanshell already has enough of those that this shouldn't be a serious argument against it.

Specifically, the operator ?. would behave as . if the LHS is not null. Otherwise, the entire expression would return null. If we're really going all-out, copying C#'s safe array dereference a?[0] syntax and CoffeeScript's a?() would also be neat.

We'd need to discuss how to handle chaining. Groovy attempts to invoke each item in the chain a?.b()?.c()?.d(), even if a is null. That means that if a ?. is accidentally missed (easy to do), a NPE is thrown. I feel this is not intuitive and that the lookup and invocation of c should never even be attempted after the first ?. check has a null LHS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions