Parameter untupling does not work with named tuples

Hello, I am not sure if this is a bug or a SIP candidate, but I have recently changed a few tuples to named tuples, and this code (parameter untupling) stopped compiling.

val x: Vector[(a: Int, b: String)] = Vector.empty

def f(a: Int, b: String): Unit = ???

x.foreach(f)

Just want to ask if this is intentional?

To me it does not seem orthogonal, as with every other Scala feature, named tuples are interchangeable with ordinary ones.

This is actually not quite true.

scala> val t = (5, "eel")
val t: (Int, String) = (5, "eel")
                                                                                
scala> val u = (n = 5, fish = "eel")
val u: (n : Int, fish : String) = (5, "eel")
                                                                                
scala> t._1
val res0: Int = 5
                                                                                
scala> u._1
-- [E008] Not Found Error: -----------------------------------------------------
1 |u._1
  |^^^^
  |value _1 is not a member of (n : Int, fish : String)
1 error found

But because

scala> t(0)
val res2: Int = 5
                                                                                
scala> u(0)
val res3: Int = 5

does work, arguably the auto-untupling ought to still work.

1 Like

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)
        }
      )
  }
}