If I do:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { t => t._1 + t._2 }
It's ok.
If I do:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { case (b, n) => b + n }
It's ok too.
But if I do:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { (b, n) => b + n }
It will not work.
Why should I use "case" keyword to use named tuples?
The type of a function argument passed to map function applied to a sequence is inferred by the type of elements in the sequence. In particular,
scenario 1:
l map { t => t._1 + t._2 }
is same asl map { t: ((String, String)): (String) => t._1 + t._2 }
but shorter, which is possible because of type inference. Scala compiler automatically inferred the type of the argument to be(String, String) => String
scenario 2: you can also write in longer form
scenario 3: a function of wrong type is passed to map, which is similar to
This situation can be understand with the
types
of inner function.First, the type syntax of parameter function for the
map
function is as follows.The first parameter function is expand to this.
This is ok. Then the second function.
This is also ok. In the failure scenario,
Lets check the types of the function
So the parameter function type is wrong.
As a solution, you can convert multiple arity functions to tuple mode and backward with the helper functions in Function object. You can do following.
Please refer Function API for further information.
The error message with 2.11 is more explanatory:
For an apply, you get "auto-tupling":
where you supplied two args instead of one.
But you don't get auto-untupling.
People have always wanted it to work that way.