Script lambdas are very similar to ordinary functional lambdas, but they return scripts. The motivation for them is very similar to that one of the ordinary lambdas and they are used in similar situations when programming in SubScript.
You can define a script lambda as follows:
var x: Int => Script[Any] = (x => [{println(x)}])
Here, we have defined a lambda that takes an Int
argument and returns a Script[Any]
. This particular lambda just prints its argument in a normal code block. You can execute this script via _execute(x(3))
(or whatever number you wish). Note that parentheses around the lambda definition are mandatory.
The Any
as the Script
‘s type parameter denotes the result type of the script. This feature is currently in development, so for now assume that all scripts have a type of Script[Any]
.
Here is an example of how you could use a script lambda:
import subscript.DSL._ // needed to bring _execute() in scope import subscript.vm._ // needed to bring Script[Any] in scope object Main { def main(args: Array[String]): Unit = _execute([live]) script.. // with the help of def script.. we define several scripts at once f(s: Int => Script[Any]) = s(3) live = f(x => [{println(x)}]) }