Totally agreed. After getting back into front end lately, it is striking that varargs are limited in Scala in that you can only spread one vararg seq, while in JS you can mix and match multiple spreads and individual elements:
JS:
const a = [1,2];
const b = [5,6];
[0, ...a, 3, 4, ...b, 7, 8]
To achieve this in scala you’d have to do something like…
val a = Seq(1,2)
val b = Seq(5,6)
Seq((0 +: a ++: 3 +: 4 +: b ++: 7 +: 8 +: Nil): _*)
Would be nicer if we could be able to do either one of:
Seq(0, ...a, 3, 4, ...b, 7, 8)
Seq(0, a:_*, 3, 4, b:_*, 7, 8)
Similarly the spread works for objects too in JS which is nice:
let a = {i: 0, j: 1}
let b = {k: 2, l: 3}
{
i: -1, // <- set default i, in case not present in `a`
...a,
...b,
x: 7,
y: 77,
k: 1, // <-- override the `k` from b
}
It’d be cool to one day be able to something like that in scala maps:
val a = Map("i" -> 0, "j" -> 1)
val b = Map("k" -> 2, "l" -> 3)
Map(
"i" -> -1,
...a,
...b,
"x" -> 7,
"y" -> 77,
"k" -> 1,
)