Hello there again! Hope I’m not being tedious for creating posts that much.
I’m struggling with creation of a function with spread arguments.
Lets say I need to interop with the function:
function onSomeEventOccurred(handler: (string, ...number[]) => void): void {
// Actual implementation is hidden
setInterval(() => {
handler("event-occurred", ...[1, 2, 3]);
}, 1000);
}
In ts, I’d use the function like so:
onSomeEventOccurred((channel, ...numbers) => {
console.log(channel, numbers);
})
In ReScript I’ve came up with the following solution:
// How to write a type for that handler?
@val
external onSomeEventOccurred: 'a => unit = "onSomeEventOccurred"
@val
external arguments: 'a = "arguments"
onSomeEventOccurred(() => {
let args = arguments
let arg0: string = args[0]
let rest: array<float> = args->Belt.Array.slice(~offset=1, ~len=Belt.Array.length(args))
Js.log2(arg0, rest)
})
Sure, I can make a wrapper for onSomeEventOccured
like so:
let onSomeEventOccurredWrapper: ((string, array<float>) => unit) => unit = handler => {
onSomeEventOccurred(() => {
let args = arguments
let arg0: string = args[0]
let rest: array<float> = args->Belt.Array.slice(~offset=1, ~len=Belt.Array.length(args))
handler(arg0, rest)
})
}
onSomeEventOccurredWrapper((channel, numbers) => {
Js.log2(channel, numbers)
})
But it still uses arguments
. Is there a better way to create a function with spread arguments without creation of a wrapper and usage of arguments?
Actual real world example would be Electron.ipcMain.on listener