Skip to content

Commit 397f52b

Browse files
committed
Initial commit
0 parents  commit 397f52b

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lazy val root = project
2+
.in(file("."))
3+
.settings(
4+
name := "scala3-tuple-cast-exception",
5+
scalaVersion := "3.0.0-RC2"
6+
)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.4.9

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.5.4")

src/main/scala/Main.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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]

0 commit comments

Comments
 (0)