Making `@variadic` work with a dynamically generated array arg

In the following code snippet, how do I make the latter two work like the first?

@module("path") @variadic
external join: array<string> => string = "join"

// 1. works as expected
let v1 = join(["1", "2"])

// 2. Compiles to:
// var v2 = Caml_splice_call.spliceApply(Path.join, [args2]);
let args2 = [1, 2]->Array.map(v => v->Int.toString)
let v2 = join(args2)

// 3. Compiles to:
// var v3 = Caml_splice_call.spliceApply(Path.join, [args3]);
let args3 = []
[1, 2]->Array.forEach(v => args3->Array.push(v->Int.toString))
let v3 = join(args3)

Playground link

@variadic works with dynamic array arg, that’s actually why Caml_splice_call.spliceApply is used.

If you select v12 you’ll notice it now compiles to a cleaner:

Path.join(...args2)

Playground link

2 Likes