Last active
February 8, 2025 08:46
-
-
Save ghostdogpr/6f2ca0939c67765a0657a255ed653765 to your computer and use it in GitHub Desktop.
Sum Type Derivation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.annotation.nowarn | |
| import scala.deriving.Mirror | |
| trait TC[A] { | |
| def show(a: A): Unit | |
| } | |
| @nowarn | |
| inline def gen[A](using m: Mirror.SumOf[A]): TC[A] = { | |
| val subTypes = compiletime.summonAll[Tuple.Map[m.MirroredElemTypes, TC]] | |
| new TC[A] { | |
| def show(a: A): Unit = | |
| subTypes(m.ordinal(a)).asInstanceOf[TC[A]].show(a) | |
| } | |
| } | |
| sealed trait Parent | |
| case class A() extends Parent | |
| object A { | |
| given TC[A] = new TC[A] { | |
| def show(a: A): Unit = println("a") | |
| } | |
| } | |
| case class B() extends Parent | |
| object B { | |
| given TC[B] = new TC[B] { | |
| def show(a: B): Unit = println("b") | |
| } | |
| } | |
| object C extends Parent { | |
| given TC[C.type] = new TC[C.type] { | |
| def show(a: C.type): Unit = println("c") | |
| } | |
| } | |
| val tc = gen[Parent] | |
| tc.show(A()) | |
| tc.show(B()) | |
| tc.show(C) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for pointing me to using
summonAllinstead of recursive inlining - for example, the following will hit "Maximal number of successive inlines (32) exceeded" with scala 3.3.5, but will compile with 3.4.0 and above:Seems like this commit is what changed the behavior.
Since library authors are expected to stay on 3.3.x, it might be worth to actively point this out to any authors of libs that could improve the "max-inlines"-behavior of their lib. (Good thing is: the new behavior will apply if a lib using
summonAllthat is compiled with 3.3.x is used from a project using 3.4.0 or higher.)