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