The UNO API supports inout
and out
parameters -- a variable is passed into a function, the function modifies the variable, and the modified variable is then available outside the function.
Javascript doesn't support this type of parameter call, so the Automation bridge enables a workaround -- passing in an array; the element at index 0
is modified, and can be retrieved outside the function:
// for inout parameters
let inout = ['input value'];
obj.func(inout);
console.log(inout[0]); //could print 'new value'
For inout
parameters, since an initial value is expected, the function could be declared with a tuple type:
declare interface Obj {
func(inout: [string]): void;
}
However, an out
parameter doesn't need an initial value:
// Javascript
let out = [];
obj.func1(out);
console.log(out[0]); // could print 'new value'
AFAIK there is no way to represent the empty tuple type, and an empty array is incompatible with the tuple type. If I declare the function argument with a tuple type, I cannot pass in an empty array:
// Typescript
declare interface Obj {
func1(out: [string]): void;
}
let out = [];
obj.func1(out);
// Argument of type 'any[]' is not assignable to parameter of type '[string]'.
// Property '0' is missing in type 'any[]'.
There are two hacks I could use:
either subvert the type system using
any
:let out: [string] = [] as any;
or use an initialized array:
let out: [string] = [''];
Is there any less "hacky" way to do this?
You can declare the
inout
parameter asinout: string[]
, which enables you to pass on an empty array.e.g.: