Fork me on GitHub
#other-languages
<
2019-05-30
>
Mno12:05:41

In scala I have a function that takes a series of arguments, and I have a Seq with the values that correspond exactly, is there a nice way to pass the sequence as the arguments?

val x = Seq(1,2,3)
def test(a: Int, b: Int, c:Int) {
    println(a)
}
test(x.something?)

borkdude12:05:01

@hobosarefriends I suspect (but I’m not sure) that this will only work with varargs functions:

def printAll(strings: String*) {
  strings.foreach(println)
}
val x = Seq("1","2","3")
printAll(x:_*)

borkdude12:05:23

Since Scala can’t determine the length of the Seq at compile time (in general), I don’t think it will accept passing it to a fixed arity function

Mno12:05:58

Aww.. well I guess I’ll just have to destructure it and pass them seperately. Thanks for the help!

borkdude12:05:39

There’s also a Scala gitter on which people are sometimes helpful, might make sense to check it there too 🙂

Mno13:05:17

I could but I’m afraid of everyone there 😆

borkdude13:05:24

to illustrate what I meant earlier:

try { val Seq(a,b,c,d) = Seq(1,2,3); d } catch { case e: Exception => 1 }
res6: Int = 1
it can only decide at runtime if the pattern match will succeed

Mno13:05:00

aaaaah, yeah that makes sense.