Skip to content

Liftable derivation #7327

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

Closed

Conversation

anatoliykmetyuk
Copy link
Contributor

No description provided.

@anatoliykmetyuk
Copy link
Contributor Author

anatoliykmetyuk commented Sep 27, 2019

Blocked by #7322 (since unable to use the derivation givens in stdlib – or any separate compilation for that matter).

@anatoliykmetyuk
Copy link
Contributor Author

Rebased on top of #7333

@@ -305,4 +306,27 @@ object Liftable {
'{ BigDecimal(${Expr(x.toString)}) }
}

inline given productLiftable[T <: Product: Type](given
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid duplication of most of this logic you can split it into

inline given [T <: Product: Type](given
    m : Mirror.ProductOf[T],
    em: Expr[Mirror.ProductOf[T]]): Liftable[T] =
      productLiftable(given m, em)(summonAll[Tuple.Map[m.MirroredElemTypes, Liftable]])

private def productLiftable[T <: Product: Type](given
    m : Mirror.ProductOf[T],
    em: Expr[Mirror.ProductOf[T]])(liftables: List[Any]): Liftable[T] = new Liftable[T] {
  def toExpr(x: T) =
    val genRepr = Tuple.fromProductTyped(x)
    val elemsWithLiftables = liftables.zip(genRepr.asInstanceOf[Product].productIterator.toList)
    val tupleOfExprs = elemsWithLiftables.map {
      case (l: Liftable[a], x) => l.toExpr(x.asInstanceOf[a])
    }
    val exprOfTuple = Expr.ofTuple(tupleOfExprs)
    '{$em.fromProduct($exprOfTuple.asInstanceOf[Product])}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid inlining too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants