camunda / feel-scala

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

Pass a reference to a function as a function argument #651

Open saig0 opened 1 year ago

saig0 commented 1 year ago

Describe the bug I want to sort a list of items using the sort() function. To improve the readability of the expression, I want to define the sorting function before, inside the same context.

{
  by_prio: function (x,y) x.prio < y.prio,
  sorted: sort(items, by_prio)
}

But it doesn't work and the sort() function returns null.

If I inline the sorting function then it works as expected.

{
  sorted: sort(items, function (x,y) x.prio < y.prio)
}

Note that this is a general issue that the function definition can not be accessed as a variable. But it can be invoked as a function.

{
  greeting: function (x) "Hello " + string(x),
  test: greeting("FEEL")
}
// works: "Hello FEEL"

{
  greeting: function (x) "Hello " + string(x),
  test: greeting instance of function
}
// failed to evaluate expression '{
//  greeting: function (x) "Hello " + string(x),
//  test: greeting instance of function
// }': no variable found for name 'greeting'

To Reproduce Steps to reproduce the behavior:

  1. Evaluate the following expression:
    {
    by_prio: function (x,y) x.prio < y.prio,
    sorted: sort(items, by_prio)
    }

    With the context:

    {"items": [
    {"id": 1, "prio": 1},
    {"id": 2, "prio": 3},
    {"id": 3, "prio": 2}
    ]}
  2. Verify that the result is null

Expected behavior I can pass a reference to a function definition as a function argument to the sort() function.

In general, I can access a reference to a function definition, for example, to check that the type of the reference is a function.

Environment