The UNO API offers support for inout
and out
parameters. In this scenario, a variable is passed into a function, the function modifies the variable, and the updated value of the variable can be accessed outside the function.
Javascript lacks native support for this type of parameter passing, so the Automation bridge provides a workaround by allowing the use of arrays. The value at index 0
in the array is modified within the function and can then be retrieved externally:
// Example for inout parameters
let inout = ['input value'];
obj.func(inout);
console.log(inout[0]); // This might output 'new value'
For functions with inout
parameters, a default initial value is typically required. Such functions could be declared using tuple types:
declare interface Obj {
func(inout: [string]): void;
}
On the other hand, an out
parameter does not necessitate an initial value:
// Javascript
let out = [];
obj.func1(out);
console.log(out[0]); // This could print 'new value'
Currently, there seems to be no way to represent an empty tuple type directly, and an empty array does not match the expected tuple type. Attempting to pass an empty array when the function argument is declared as a tuple type results in a mismatch error:
// Typescript
declare interface Obj {
func1(out: [string]): void;
}
let out = [];
obj.func1(out);
// Error: Argument of type 'any[]' is not assignable to parameter of type '[string]'.
// Property '0' is missing in type 'any[]'.
There are a couple of workarounds that can be used:
Bypass the type system using
any
:let out: [string] = [] as any;
Alternatively, initialize the array with a single element:
let out: [string] = [''];
Is there a more elegant solution to handle this without resorting to "hacky" methods?