|
| 1 | +package scala.deriving |
| 2 | + |
| 3 | +/** Mirrors allows typelevel access to enums, case classes and objects, and their sealed parents. |
| 4 | + */ |
| 5 | +sealed trait Mirror { |
| 6 | + |
| 7 | + /** The mirrored *-type */ |
| 8 | + type MirroredMonoType |
| 9 | + |
| 10 | + /** The name of the type */ |
| 11 | + type MirroredLabel <: String |
| 12 | + |
| 13 | + /** The names of the product elements */ |
| 14 | + type MirroredElemLabels <: Tuple |
| 15 | +} |
| 16 | + |
| 17 | +object Mirror { |
| 18 | + |
| 19 | + /** The Mirror for a sum type */ |
| 20 | + trait Sum extends Mirror { self => |
| 21 | + /** The ordinal number of the case class of `x`. For enums, `ordinal(x) == x.ordinal` */ |
| 22 | + def ordinal(x: MirroredMonoType): Int |
| 23 | + } |
| 24 | + |
| 25 | + /** The Mirror for a product type */ |
| 26 | + trait Product extends Mirror { |
| 27 | + |
| 28 | + /** Create a new instance of type `T` with elements taken from product `p`. */ |
| 29 | + def fromProduct(p: scala.Product): MirroredMonoType |
| 30 | + } |
| 31 | + |
| 32 | + trait Singleton extends Product { |
| 33 | + type MirroredMonoType = this.type |
| 34 | + type MirroredType = this.type |
| 35 | + type MirroredElemTypes = EmptyTuple |
| 36 | + type MirroredElemLabels = EmptyTuple |
| 37 | + def fromProduct(p: scala.Product): MirroredMonoType = this |
| 38 | + } |
| 39 | + |
| 40 | + /** A proxy for Scala 2 singletons, which do not inherit `Singleton` directly */ |
| 41 | + class SingletonProxy(val value: AnyRef) extends Product { |
| 42 | + type MirroredMonoType = value.type |
| 43 | + type MirroredType = value.type |
| 44 | + type MirroredElemTypes = EmptyTuple |
| 45 | + type MirroredElemLabels = EmptyTuple |
| 46 | + def fromProduct(p: scala.Product): MirroredMonoType = value |
| 47 | + } |
| 48 | + |
| 49 | + type Of[T] = Mirror { type MirroredType = T; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple } |
| 50 | + type ProductOf[T] = Mirror.Product { type MirroredType = T; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple } |
| 51 | + type SumOf[T] = Mirror.Sum { type MirroredType = T; type MirroredMonoType = T; type MirroredElemTypes <: Tuple } |
| 52 | +} |
0 commit comments