Skip to content

Mirror

What a Mirror Is Mirror.ProductOf[A] acts like a typeclass instance describing A as a product type: a value containing several components together. For example, a Person contains a name and an age:

import scala.deriving.Mirror
case class Person(name: String, age: Int)
val personMirror = summon[Mirror.ProductOf[Person]]

No derives clause is necessary. Scala synthesizes the mirror for eligible case classes automatically.

It is not special merely because it is available during compilation: ordinary typeclass resolution also happens during compilation. What distinguishes mirrors is compiler synthesis and precise structural metadata encoded as type members.

both ordinary case classes and concrete tuple types have Mirror.ProductOf instances. The key is to distinguish the value, its type, and the mirror describing that type.

Types and Values For this example, the mirror exposes these type members:

Member Meaning For Person
MirroredType Type being described Person
MirroredMonoType Type used by runtime operations Person
MirroredLabel Type name as a singleton string type "Person"
MirroredElemTypes Field types, in constructor order (String, Int)
MirroredElemLabels Field names as singleton string types ("name", "age")

These are types, not object properties containing runtime values. For example, personMirror.MirroredElemTypes is used in a type position.

A mirror also has runtime behavior:

val person: Person = personMirror.fromProduct(("Ada", 37))

fromProduct constructs a Person using the supplied product’s elements. It takes a scala.Product, so its argument is not statically restricted to the correct field types and arity. Generic code must ensure they match.

It does not fill in omitted constructor defaults. Supplying an incomplete product is not equivalent to calling Person with omitted arguments.

Where Tuple Fits Tuple is not exclusively a compile-time representation. Scala has both tuple values and tuple types:

val values: (String, Int) = ("Ada", 37)
type FieldTypes = (String, Int)
type FieldNames = ("name", "age")
  • values is an ordinary runtime tuple.
  • FieldTypes is a tuple type describing a string followed by an integer.
  • FieldNames is a tuple type whose elements are singleton string types.

Mirrors use tuple types as type-level lists, including when describing case classes. That does not turn the case class itself into a tuple.

For recursive type-level processing, (String, Int) can also be expressed as:

type FieldTypes = String *: Int *: EmptyTuple

This lets generic derivation process one element type at a time.

Both Are Products These both work:

val caseClassMirror = summon[Mirror.ProductOf[Person]]
val tupleMirror = summon[Mirror.ProductOf[(String, Int)]]

Both describe two components with types (String, Int), but reconstruct different types: Person versus (String, Int). An arbitrary class does not automatically get a mirror, nor does merely extending scala.Product guarantee one.

For contrast, Mirror.SumOf[A] describes alternatives, such as an enum value being one case or another.

In Your Decoder At src/sfenv/rules/util.scala:21, constValueTuple[mirror.MirroredElemLabels] converts the compile-time tuple of singleton field-name types into a tuple of string values. Those values become the set used to partition input fields.

The distinction is: the mirror describes the structure, tuple types encode its element metadata, and generated code uses that metadata to operate on runtime values.