Last active
December 15, 2021 16:01
-
-
Save mpilquist/bb187ffcded9ac34f88de258ff4506bf to your computer and use it in GitHub Desktop.
This file contains 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
// using scala 3.1.0 | |
import scala.deriving.Mirror | |
object Mirrors: | |
extension [A <: Product](a: A)(using m: Mirror.ProductOf[A]) | |
def toTuple: m.MirroredElemTypes = Tuple.fromProductTyped(a) | |
extension [T <: Tuple](t: T) | |
def to[A <: Product](using m: Mirror.ProductOf[A], ev: m.MirroredElemTypes =:= T): A = | |
m.fromProduct(t) | |
@main def main = | |
import Mirrors.* | |
case class Foo(x: Int, y: String, z: Boolean) | |
println(Foo(42, "Hello", true).toTuple) | |
println((42, "Hello", true).to[Foo]) | |
// Alternatively, maybe this lands in Scala 3.2+ https://github.com/lampepfl/dotty/pull/14114 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment