File tree Expand file tree Collapse file tree 5 files changed +50
-0
lines changed Expand file tree Collapse file tree 5 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ target /
Original file line number Diff line number Diff line change
1
+ lazy val root = project
2
+ .in(file(" ." ))
3
+ .settings(
4
+ name := " scala3-tuple-cast-exception" ,
5
+ scalaVersion := " 3.0.0-RC2"
6
+ )
Original file line number Diff line number Diff line change
1
+ sbt.version =1.4.9
Original file line number Diff line number Diff line change
1
+ addSbtPlugin(" ch.epfl.lamp" % " sbt-dotty" % " 0.5.4" )
Original file line number Diff line number Diff line change
1
+ import scala .deriving .*
2
+ import scala .compiletime .{erasedValue , summonInline }
3
+
4
+ case class Simple (a : String , b : Boolean ) derives Printable
5
+ case class SimpleT (a : (String , Boolean )) derives Printable
6
+
7
+ @ main def hello : Unit = {
8
+
9
+ summon[Printable [Simple ]].print // Prints STRING BOOLEAN as expected
10
+
11
+ summon[Printable [SimpleT ]].print // java.lang.ClassCastException: SimpleT$$anon$1 cannot be cast to scala.deriving.Mirror$Product
12
+
13
+ }
14
+
15
+ trait Printable [T ]:
16
+ def print : Unit
17
+
18
+ object Printable :
19
+
20
+ given Printable [String ] with
21
+ def print : Unit = println(" STRING" )
22
+
23
+ given Printable [Boolean ] with
24
+ def print : Unit = println(" BOOLEAN" )
25
+
26
+ def printProduct [T ](p : Mirror .ProductOf [T ], elems : => List [Printable [_]]): Printable [T ] =
27
+ new Printable [T ]:
28
+ def print : Unit =
29
+ elems.foreach(_.print)
30
+
31
+ inline given derived [T ](using m : Mirror .Of [T ]): Printable [T ] =
32
+ val elemInstances = summonAllPrintable[m.MirroredElemTypes ]
33
+ inline m match
34
+ case p : Mirror .ProductOf [T ] => printProduct(p, elemInstances)
35
+
36
+ end Printable
37
+
38
+ inline def summonAllPrintable [T <: Tuple ]: List [Printable [_]] =
39
+ inline erasedValue[T ] match
40
+ case _ : EmptyTuple => Nil
41
+ case _ : (t *: ts) => summonInline[Printable [t]] :: summonAllPrintable[ts]
You can’t perform that action at this time.
0 commit comments