It works when written
scala> x.foreach(f(_, _))
There’s a coda to the doc that explains why this doesn’t work:
val combiner: (Int, Int) => Int = _ + _
xs.map(combiner) // Type Mismatch
It looks like eta expansion f stands in the way of parameter untupling here.
The correct case, where apply is an extension method:
scala> x.foreach(f(_,_))
[[syntax trees at end of typer]] // rs$line$7
package <empty> {
final lazy module val rs$line$7: rs$line$7 = new rs$line$7()
final module class rs$line$7() extends Object() { this: rs$line$7.type =>
val res0: Unit =
x.foreach[Unit]((x$1: (a : Int, b : String)) =>
{
val _$1: Int =
NamedTuple.apply[(("a" : String), ("b" : String)), (Int, String)](
x$1)(0)
val _$2: String =
NamedTuple.apply[(("a" : String), ("b" : String)), (Int, String)](
x$1)(1)
f(_$1, _$2)
}
)
}
}
Compare:
scala> x.foreach(f)
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |x.foreach(f)
| ^
| Found: ((Int, String)) => Unit
| Required: (a : Int, b : String) => Unit
|
| longer explanation available when compiling with `-explain`
[[syntax trees at end of typer]] // rs$line$9
package <empty> {
final lazy module val rs$line$9: rs$line$9 = new rs$line$9()
final module class rs$line$9() extends Object() { this: rs$line$9.type =>
val res1: <error unspecified error> =
x.foreach[Unit]((x$1: Tuple2[Int, String]) =>
{
val a: Int = x$1.apply[(Int, String)](0)
val b: String = x$1.apply[(Int, String)](1)
f(a, b)
}
)
}
}
It must be a bug.
The unnamed tuple case for comparison:
scala> y.foreach(f)
[[syntax trees at end of typer]] // rs$line$8
package <empty> {
final lazy module val rs$line$8: rs$line$8 = new rs$line$8()
final module class rs$line$8() extends Object() { this: rs$line$8.type =>
val res1: Unit =
y.foreach[Unit]((x$1: Tuple2[Int, String]) =>
{
val a: Int = x$1._1
val b: String = x$1._2
f(a, b)
}
)
}
}