Skip to content

Commit 2699974

Browse files
authored
Merge pull request #10404 from dotty-staging/refactor-scala-deriving
Refactor scala.deriving
2 parents 43f72c2 + 0f1a331 commit 2699974

File tree

3 files changed

+70
-72
lines changed

3 files changed

+70
-72
lines changed

library/src/scala/deriving.scala

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scala.deriving
2+
3+
/** Helper class to turn arrays into products */
4+
class ArrayProduct(val elems: Array[AnyRef]) extends Product {
5+
def this(size: Int) = this(new Array[AnyRef](size))
6+
def canEqual(that: Any): Boolean = true
7+
def productElement(n: Int): Any = elems(n)
8+
def productArity: Int = elems.length
9+
override def productIterator: Iterator[Any] = elems.iterator
10+
def update(n: Int, x: Any): Unit = elems(n) = x.asInstanceOf[AnyRef]
11+
}
12+
13+
/** The empty product */
14+
object EmptyProduct extends ArrayProduct(Array.emptyObjectArray)
15+
16+
/** Helper method to select a product element */
17+
def productElement[T](x: Any, idx: Int): T =
18+
x.asInstanceOf[Product].productElement(idx).asInstanceOf[T]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)