File tree Expand file tree Collapse file tree 4 files changed +33
-5
lines changed
library/src/scala/deriving
cmdTest-sbt-tests/sourcepath-with-inline/src/main/scala/a Expand file tree Collapse file tree 4 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -49,4 +49,13 @@ object Mirror {
49
49
type Of [T ] = Mirror { type MirroredType = T ; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple }
50
50
type ProductOf [T ] = Mirror .Product { type MirroredType = T ; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple }
51
51
type SumOf [T ] = Mirror .Sum { type MirroredType = T ; type MirroredMonoType = T ; type MirroredElemTypes <: Tuple }
52
+
53
+ extension [T ](p : ProductOf [T ])
54
+ /** Create a new instance of type `T` with elements taken from product `a`. */
55
+ def fromProductTyped [A <: scala.Product ](a : A )(using m : ProductOf [A ], ev : p.MirroredElemTypes =:= m.MirroredElemTypes ): T =
56
+ p.fromProduct(a)
57
+
58
+ /** Create a new instance of type `T` with elements taken from tuple `t`. */
59
+ def fromTuple (t : p.MirroredElemTypes ): T =
60
+ p.fromProduct(t)
52
61
}
Original file line number Diff line number Diff line change
1
+ package a
2
+
3
+ object Foo : // note that `Foo` is defined in `zz.scala`
4
+ class Local
5
+ inline def foo (using Local ): Nothing =
6
+ ???
Original file line number Diff line number Diff line change 1
- import reflect . Generic
1
+ import deriving . MirrorOf
2
2
3
- sealed trait A derives Generic // error: cannot take shape, it has anonymous or inaccessible subclasses
3
+ sealed trait A
4
4
5
5
object A {
6
6
def f () = {
@@ -9,17 +9,28 @@ object A {
9
9
}
10
10
}
11
11
12
- sealed trait B derives Generic // error: cannot take shape, its subclass class D is not a case class
12
+ def aMirror = summon[Mirror .Of [A ]] // error: cannot take shape, it has anonymous or inaccessible subclasses
13
+
14
+ sealed trait B
13
15
14
16
class D (x : Int , y : String ) extends B
15
17
18
+ def bMirror = summon[Mirror .Of [B ]] // error: cannot take shape, its subclass class D is not a case class
16
19
17
- class E derives Generic // error: cannot take shape, it is neither sealed nor a case class
20
+ class E
21
+ def eMirror = summon[Mirror .Of [E ]] // error: cannot take shape, it is neither sealed nor a case class
18
22
19
- sealed trait F derives Generic // error: cannot take shape, it has anonymous or inaccessible subclasses
23
+ sealed trait F
24
+ def fMirror = summon[Mirror .Of [F ]] // error: cannot take shape, it has anonymous or inaccessible subclasses
20
25
21
26
object G {
22
27
def f () = {
23
28
case class H () extends F
24
29
}
25
30
}
31
+
32
+ case class I (x : Int , y : String )
33
+ object I :
34
+ def f = summon[deriving.Mirror .ProductOf [I ]].fromProductTyped((1 , 2 )) // error
35
+ def g = summon[deriving.Mirror .ProductOf [I ]].fromTuple((1 , 2 )) // error
36
+
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ object Test extends App {
13
13
case class AA [X >: Null <: AnyRef ](x : X , y : X , z : String )
14
14
15
15
println(summon[Mirror .ProductOf [A ]].fromProduct(A (1 , 2 )))
16
+ summon[Mirror .ProductOf [A ]].fromProductTyped(A (1 , 2 ))
17
+ summon[Mirror .ProductOf [A ]].fromTuple((1 , 2 ))
16
18
assert(summon[Mirror .SumOf [T ]].ordinal(A (1 , 2 )) == 0 )
17
19
assert(summon[Mirror .Sum { type MirroredType = T }].ordinal(B ) == 1 )
18
20
summon[Mirror .Of [A ]] match {
You can’t perform that action at this time.
0 commit comments