Script error "Couldn't invoke 'assignValueTo' for feature param"

I have an error during assignment value to a lambda function parameter. The simpliest code for repro is:

val org.eclipse.xtext.xbase.lib.Functions$Function1 f = [
    long i |
    i += 1;
]

var long value = 0

rule "Sample Rule"
when Time is midnight then
    f.apply(value)
end

After that rule is called, next error appears in the log:

00:00:00.090 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule ‘Sample Rule’: Couldn’t invoke ‘assignValueTo’ for feature param i

That also happens for other types of variable, not only for primitive types.

Possible workaround is to create a Number Item, pass it to the function and use postUpdate method. But is it possible to do without such overhead?

I’ve had some problems using the += type operators. Do you get the same error when using i = i + 1?

Also, I’m not 100% certain but I think that Items are passed to a lambda by value, not by reference. So even if you do successfully assign a new value to i, it would actually update value. And given the error, it might be possible that the lambda parameters are passed as vals which means it wont let you ever reassign i. If I’m right, then to get what you want you will need to return the new value:

val org.eclipse.xtext.xbase.lib.Functions$Function1 f = [
    long i |
    return i + 1
]

var long value = 0

rule "Sample Rule"
when Time is midnight then
    value = f.apply(value)
end

Unless you are facing timing or performance issues, I actually recommend using that approach most of the time. You get the advantage of being able to persist the values and restore on startup so your rules can pick back up where they started and you can reference the value from other .rules files.

1 Like