Will tuple unpacking be directly supported in para

2019-04-21 04:16发布

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

?

4条回答
老娘就宠你
2楼-- · 2019-04-21 04:40

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.

查看更多
别忘想泡老子
3楼-- · 2019-04-21 04:47

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

查看更多
地球回转人心会变
4楼-- · 2019-04-21 04:57

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

查看更多
霸刀☆藐视天下
5楼-- · 2019-04-21 05:06

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
}
查看更多
登录 后发表回答