Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Last active December 15, 2021 16:01
Show Gist options
  • Save mpilquist/bb187ffcded9ac34f88de258ff4506bf to your computer and use it in GitHub Desktop.
Save mpilquist/bb187ffcded9ac34f88de258ff4506bf to your computer and use it in GitHub Desktop.
// 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