Closed
Description
import scala.deriving._
case class Foo(x: Int, y: String)
def toTuple[T <: Product](x: T)(using m: Mirror.ProductOf[T], mt: Mirror.ProductOf[m.MirroredElemTypes]) =
mt.fromProduct(x)
def weird = {
val m = summon[Mirror.ProductOf[Foo]]
val mt1 = summon[Mirror.ProductOf[(Int, String)]]
type R = (Int, String)
val mt2 = summon[Mirror.ProductOf[R]]
val mt3 = summon[Mirror.ProductOf[m.MirroredElemTypes]]
val f = Foo(1, "foo")
val g: (Int, String) = toTuple(f)// (using m, mt1)
}
Says:
-- Error: ../pg/Main.scala:20:54 -----------------------------------------------
20 | val mt3 = the[Mirror.ProductOf[m.MirroredElemTypes]]
| ^
|no implicit argument of type deriving.Mirror.ProductOf[m.MirroredElemTypes] was found for parameter x of method the in object DottyPredef
-- Error: ../pg/Main.scala:23:35 -----------------------------------------------
23 | val g: (Int, String) = toTuple(f)// given (m, mt1)
| ^
|no implicit argument of type deriving.Mirror.ProductOf[m.MirroredElemTypes] was found for parameter mt of method toTuple
two errors found
That is, we can resolve the mirror of the tuple, but not if it is obtained from another mirror. To make it work:
def weird = {
val m = the[Mirror.ProductOf[Foo]]
val mt1 = the[Mirror.ProductOf[(Int, String)]]
type R = (Int, String)
val mt2 = the[Mirror.ProductOf[R]]
// val mt3 = the[Mirror.ProductOf[m.MirroredElemTypes]]
val f = Foo(1, "foo")
val g: (Int, String) = toTuple(f) given (m, mt1)
}