I’m trying to bind to a function that takes an array, with each element being a string or array of strings. I’m using polymorphic variants to model those elements and trying to unwrap each value in the array but not sure if I’m doing something wrong or if it’s not possible right now. I can get around this with an extra function call and some magic conversions but would be great if I could do it with zero cost bindings. As a simple example here’s a copy of the documentation example replacing the int variant with a string array which works fine:
@val
external padLeft: (
string,
@unwrap
[
| #Str(string)
| #StrArr(array<string>)
],
) => string = "padLeft"
padLeft("Hello World", #Str("Message from ReScript: "))->ignore
padLeft("Hello World", #StrArr(["yup", "nope"]))->ignore
Results in this output:
padLeft("Hello World", "Message from ReScript: ");
padLeft("Hello World", [
"yup",
"nope"
]);
Here’s what I tried that could compile:
@val
external padLeft: (
string,
array<
@unwrap
[
| #Str(string)
| #StrArr(array<string>)
],
>,
) => string = "padLeft"
padLeft("Hello World", [#Str("Message from ReScript: ")])->ignore
padLeft("Hello World", [#StrArr(["yup", "nope"])])->ignore
But the annotation doesn’t have any effect:
padLeft("Hello World", [{
NAME: "Str",
VAL: "Message from ReScript: "
}]);
padLeft("Hello World", [{
NAME: "StrArr",
VAL: [
"yup",
"nope"
]
}]);
Thanks in advance for any help with this!