|
| 1 | +/* |
| 2 | +package scala |
| 3 | +
|
| 4 | +object deriving { |
| 5 | +
|
| 6 | + /** Mirrors allows typelevel access to enums, case classes and objects, and their sealed parents. |
| 7 | + */ |
| 8 | + sealed trait Mirror { |
| 9 | +
|
| 10 | + /** The mirrored *-type */ |
| 11 | + type MirroredMonoType |
| 12 | +
|
| 13 | + /** The name of the type */ |
| 14 | + type MirroredLabel <: String |
| 15 | +
|
| 16 | + /** The names of the product elements */ |
| 17 | + type MirroredElemLabels // <: Tuple // Bound removed to allow this to compile with |
| 18 | + // Scala 2. The full version of this file is in |
| 19 | + // library/src-3.x/scala/deriving.scala |
| 20 | + } |
| 21 | +
|
| 22 | + object Mirror { |
| 23 | +
|
| 24 | + /** The Mirror for a sum type */ |
| 25 | + trait Sum extends Mirror { self => |
| 26 | + /** The ordinal number of the case class of `x`. For enums, `ordinal(x) == x.ordinal` */ |
| 27 | + def ordinal(x: MirroredMonoType): Int |
| 28 | + } |
| 29 | +
|
| 30 | + /** The Mirror for a product type */ |
| 31 | + trait Product extends Mirror { |
| 32 | +
|
| 33 | + /** Create a new instance of type `T` with elements taken from product `p`. */ |
| 34 | + def fromProduct(p: scala.Product): MirroredMonoType |
| 35 | + } |
| 36 | +
|
| 37 | + trait Singleton extends Product { |
| 38 | + type MirroredMonoType = this.type |
| 39 | + type MirroredType = this.type |
| 40 | + type MirroredElemTypes = Unit |
| 41 | + type MirroredElemLabels = Unit |
| 42 | + def fromProduct(p: scala.Product) = this |
| 43 | + } |
| 44 | +
|
| 45 | + /** A proxy for Scala 2 singletons, which do not inherit `Singleton` directly */ |
| 46 | + class SingletonProxy(val value: AnyRef) extends Product { |
| 47 | + type MirroredMonoType = value.type |
| 48 | + type MirroredType = value.type |
| 49 | + type MirroredElemTypes = Unit |
| 50 | + type MirroredElemLabels = Unit |
| 51 | + def fromProduct(p: scala.Product) = value |
| 52 | + } |
| 53 | +
|
| 54 | + type Of[T] = Mirror { type MirroredType = T; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple } |
| 55 | + type ProductOf[T] = Mirror.Product { type MirroredType = T; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple } |
| 56 | + type SumOf[T] = Mirror.Sum { type MirroredType = T; type MirroredMonoType = T; type MirroredElemTypes <: Tuple } |
| 57 | + } |
| 58 | +
|
| 59 | + /** Helper class to turn arrays into products */ |
| 60 | + class ArrayProduct(val elems: Array[AnyRef]) extends Product { |
| 61 | + def this(size: Int) = this(new Array[AnyRef](size)) |
| 62 | + def canEqual(that: Any): Boolean = true |
| 63 | + def productElement(n: Int) = elems(n) |
| 64 | + def productArity = elems.length |
| 65 | + override def productIterator: Iterator[Any] = elems.iterator |
| 66 | + def update(n: Int, x: Any) = elems(n) = x.asInstanceOf[AnyRef] |
| 67 | + } |
| 68 | +
|
| 69 | + /** The empty product */ |
| 70 | + object EmptyProduct extends ArrayProduct(Array[AnyRef]()) |
| 71 | +
|
| 72 | + /** Helper method to select a product element */ |
| 73 | + def productElement[T](x: Any, idx: Int) = |
| 74 | + x.asInstanceOf[Product].productElement(idx).asInstanceOf[T] |
| 75 | +} |
| 76 | +*/ |
0 commit comments