Lamdba in rules file

Is it possible to use Lambdas which are defined in another rules files?

So Lambdas are not useful for my situation. I will go with Proxy Items

I’ve never tried it and can’t guarantee it will work, but theoretically you should be able to put a lambda into the shared cache. Back when I wrote that post there wasn’t a shared cache. See Textual Rules | openHAB.

I can’t remember if it was added to Rules DSL before or after 3.4 release so if you see errors saying sharedCache is not defined you’ll have to wait for OH 4 to use it.

But IMO if you are heading down this path it’s a strong indication you’ve outgrown Rules DSL and one of the other languages might be a better fit.

Thanks for your remarks.
Do you have some lines of code how to use the “shared cache”?

I linked to the docs that talk about the shared cache which includes

For further documentation on using the cache see here

which includes examples. Do you have specific questions after reading those docs and examples?

Some rule languages might not offer direct support for referencing lambdas across files. In this case, you have two options:

  • Define the Lambda in Each File: This can lead to code duplication, but ensures your rules function independently.
  • Create a Function Instead: If the lambda is complex, consider defining it as a full-fledged function within the same rule file. This promotes reusability without relying on external definitions.

Believe this is true for all the supported rules languages. A lambda is an anonymous function and as such it is really not sharable. A lambda can be assigned to a variable to de-anonymize it but except in Rules DSL which doesn’t support functions at all, you may as well just define a function in the first place.

And if you’ve defined a function that you want to use across multiple files, you may as well put them into a library you can import into your various files. JS, jRuby, and Jython all support this. Rules DSL does not.