开发者

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

?


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.


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


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
}


Another approach would be to make parameter lists and tuples the same thing in the language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜