other-languages

here be heresies and things we have to use for work
Mno 2019-05-30T12:21:41.001700Z

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?)

borkdude 2019-05-30T12:31:01.002500Z

@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:_*)

borkdude 2019-05-30T12:31:23.003Z

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

Mno 2019-05-30T12:33:58.004200Z

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

borkdude 2019-05-30T12:35:39.004600Z

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

Mno 2019-05-30T13:00:17.005100Z

I could but I’m afraid of everyone there 😆

borkdude 2019-05-30T13:32:24.005800Z

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

Mno 2019-05-30T13:34:00.006800Z

aaaaah, yeah that makes sense.