问题
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
?
回答1:
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.
回答2:
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
回答3:
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
}
回答4:
Another approach would be to make parameter lists and tuples the same thing in the language.
来源:https://stackoverflow.com/questions/6399824/will-tuple-unpacking-be-directly-supported-in-parameter-lists-in-scala