camunda / feel-scala

FEEL parser and interpreter written in Scala
https://camunda.github.io/feel-scala/
Apache License 2.0
124 stars 51 forks source link

New function to deal with optional values #618

Closed saig0 closed 1 year ago

saig0 commented 1 year ago

Is your feature request related to a problem? Please describe. If a process has optional variables then the FEEL expressions (e.g. variable mappings) need to deal with optional/nullable variables/values. Otherwise, the expression may fail.

Currently, the best option is to use an if-then-else block with a null-check.

if x != null then x else "default_value"

// or

if is defined(x) then x else "default_value"

But it is very cumbersome.

This issue is also relevant to the integration with forms. In a form, a field may be optional or shown conditionally. The process must deal with these optional form values.

Describe the solution you'd like There is an easy way to deal with optional/nullable values. For example, by adding a new built-in function:

// signature
get or default(value: Any, default: Any) 

// usage
get or default("this", "default") // -> "this"

get or default(null, "default")  // -> "default"

Alternative function names: get or else()

Alternative solutions

Instead of adding a built-in function, it would be handy to include this optional/nullable handling natively in expressions.

We have examples in other programming languages. For example:

Kotlin:

x ?: "default" 

JavaScript:

x || "default"

x ?? "default"

Related issues