forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathderiving.scala
More file actions
34 lines (29 loc) · 868 Bytes
/
deriving.scala
File metadata and controls
34 lines (29 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
sealed trait T
object T
case class A(x: Int, y: Int) extends T
case object B extends T
sealed trait U // U MUST NOT have a companion here!
case class C() extends U
object Test extends App {
import deriving._
case class AA[X >: Null <: AnyRef](x: X, y: X, z: String)
println(summon[Mirror.ProductOf[A]].fromProduct(A(1, 2)))
assert(summon[Mirror.SumOf[T]].ordinal(A(1, 2)) == 0)
assert(summon[Mirror.Sum { type MirroredType = T }].ordinal(B) == 1)
summon[Mirror.Of[A]] match {
case m: Mirror.Product =>
println(m.fromProduct(A(1, 2)))
}
summon[Mirror.Of[B.type]] match {
case m: Mirror.Product =>
println(m.fromProduct(EmptyTuple))
}
summon[Mirror.Of[T]] match {
case m: Mirror.SumOf[T] =>
println(m.ordinal(B))
}
summon[Mirror.Of[U]] match {
case m: Mirror.SumOf[U] =>
println(m.ordinal(C()))
}
}