Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions core/shared/src/main/scala-3/monocle/internal/IsoFields.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package monocle.internal

import monocle.Iso
import monocle.{Iso, PIso}
import scala.quoted.{quotes, Expr, Quotes, Type}
import scala.deriving.Mirror

object IsoFields {
transparent inline def apply[S <: Product](using mirror: Mirror.ProductOf[S]): Iso[S, Tuple] =
transparent inline def apply[S <: Product](using mirror: Mirror.ProductOf[S]): PIso[S, S, ? <: Tuple, ? <: Tuple] =
${ IsoFieldsImpl.apply[S]('mirror) }
}

private[monocle] object IsoFieldsImpl {

def apply[S <: Product](mirror: Expr[Mirror.ProductOf[S]])(using Quotes, Type[S]): Expr[Iso[S, Tuple]] = {

def whitebox[A <: Tuple](e: Expr[Iso[S, A]]): Expr[Iso[S, Tuple]] =
e.asInstanceOf[Expr[Iso[S, Tuple]]]

def apply[S <: Product](mirror: Expr[Mirror.ProductOf[S]])(using Quotes, Type[S]): Expr[PIso[S, S, ? <: Tuple, ? <: Tuple]] =
mirror match {
case '{ type a <: Tuple; $m: Mirror.ProductOf[S] { type MirroredElemTypes = `a` } } =>
whitebox('{
'{
val f: S => a = Tuple.fromProductTyped(_)(using $m)
val g: a => S = $m.fromProduct(_)
Iso[S, a](f)(g)
})
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait MacroSyntax {
* Case classes with 0 fields will correspond with `EmptyTuple`, 1 with `Tuple1[field type]`, 2 or more with a
* tuple of all field types in the same order as the fields themselves.
*/
transparent inline def fields[S <: Product: Mirror.ProductOf]: Iso[S, Tuple] =
transparent inline def fields[S <: Product: Mirror.ProductOf]: PIso[S, S, ? <: Tuple, ? <: Tuple] =
IsoFields[S]
}

Expand Down