add

open fun add(name: String, value: Any, vararg types: KType)

Adds a named variable with an associated value and optional type parameters to the script context.

Validates the name and that the number of type parameters matches the value's generic type parameter count.

Parameters

name

the variable name to bind in the script

value

the value to associate with the variable

types

the type parameters for generic types (e.g., for List<String>, pass typeOf<String>()). A value whose own class cannot be named in generated code, and takes no type parameters, may still be given some when a public class or interface it implements takes that many; it is then declared as that type (a lambda as a Function1<A, B>, the value of Comparator.naturalOrder() as a Comparator<T>)

Throws

if the name is not a valid identifier, the value is a local/anonymous class, or the type parameter count is invalid