Skip to content

Fix derivation macro example #15026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
19 changes: 10 additions & 9 deletions docs/_docs/reference/contextual/derivation-macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ given derived[T: Type](using Quotes): Expr[Eq[T]] =
ev match
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
val elemInstances = summonAll[elementTypes]
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
case (acc, (elem, index)) =>
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}
'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
}

'{ eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)}) }
def eqProductBody(x: Expr[Product], y: Expr[Product])(using Quotes): Expr[Boolean] = {
elemInstances.zipWithIndex.foldLeft(Expr(true)) {
case (acc, ('{ $elem: Eq[t] }, index)) =>
val indexExpr = Expr(index)
val e1 = '{ $x.productElement($indexExpr).asInstanceOf[t] }
val e2 = '{ $y.productElement($indexExpr).asInstanceOf[t] }
'{ $acc && $elem.eqv($e1, $e2) }
}
}
'{ eqProduct((x: T, y: T) => ${eqProductBody('x.asExprOf[Product], 'y.asExprOf[Product])}) }

// case for Mirror.ProductOf[T]
// ...
Expand Down
16 changes: 8 additions & 8 deletions tests/run-macros/i8007/Macro_3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ object Eq {
ev match {
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
val elemInstances = summonAll[elementTypes]
def eqProductBody(x: Expr[T], y: Expr[T])(using Quotes): Expr[Boolean] = {
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
case (acc, (elem, index)) =>
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}

'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
def eqProductBody(x: Expr[Product], y: Expr[Product])(using Quotes): Expr[Boolean] = {
elemInstances.zipWithIndex.foldLeft(Expr(true)) {
case (acc, ('{ $elem: Eq[t]}, index)) =>
val indexExpr = Expr(index)
val e1 = '{ $x.productElement($indexExpr).asInstanceOf[t] }
val e2 = '{ $y.productElement($indexExpr).asInstanceOf[t] }
'{ $acc && $elem.eqv($e1, $e2) }
}
}
'{
eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)})
eqProduct((x: T, y: T) => ${eqProductBody('x.asExprOf[Product], 'y.asExprOf[Product])})
}

case '{ $m: Mirror.SumOf[T] { type MirroredElemTypes = elementTypes }} =>
Expand Down