Skip to content

can't summon mirror for MirroredElemTypes as it is generic tuple #7049

Closed
@anatoliykmetyuk

Description

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions