Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will tuple unpacking be directly supported in parameter lists in Scala?

In Haskell you can write:

x :: (Int,Int) -> Int
x (p,s) = p

In Scala you would write:

def x(a: (Int, Int)) = a._1

or:

def x(a: (Int, Int)) = a match {
    case (p, s) => p
}

Why not have something like

def x(_: (p: Int, s: Int)) = p

or

def x(foo: (p @ Int, s @ Int)) = p

?

like image 594
letmaik Avatar asked Jun 19 '11 00:06

letmaik


3 Answers

The feature you're looking for is called destructuring and, in it's general form, would go well beyond just tuple unpacking. I've often found myself wishing that Scala had it since it's such a natural extension of the pattern matching syntax:

def first((f: Int, l: Int)) = f
def displayName(Person(first, last)) = last + ", " + first

Destructuring is (sort of) present in the form of variable/value definitions:

val (f, l) = tuple
val Person(first, last) = person

Unfortunately, there are some type safety issues around such definitions that I think make it unlikely that you'll see destructuring in parameter lists any time soon.

like image 121
Aaron Novstrup Avatar answered Nov 20 '22 06:11

Aaron Novstrup


You could create a function that receives an argument list which corresponds to the types of the Tuple, apply Function.tupled to that function and then apply the tuple:

scala> def fun(x:Int,y:Int)=x+y
fun: (x: Int,y: Int)Int

scala> val tuple = (1,2)
tuple: (Int, Int) = (1,2)

scala> Function.tupled(fun _)(tuple)
res9: Int = 3

This way you achieve a valid workaround for your problem

like image 39
JaimeJorge Avatar answered Nov 20 '22 08:11

JaimeJorge


That's called multiple dispatch, and it is not supported by JVM. Scala could rewrite a method to make the explicit match unnecessary, though, but it is not a priority -- or, as far as I know, even planned -- to do so.

Interestingly, it is supported for functions, in a sense, with the restriction that all variants must appear together. For example:

def x: ((Int, Int)) => Int = {
    case (p, _) => p
}
like image 28
Daniel C. Sobral Avatar answered Nov 20 '22 08:11

Daniel C. Sobral