Skip to content
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

Add TransformerClass to avoid multiple anonymous classes creation during macro expansion. #637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package io.scalaland.chimney.internal.compiletime

import io.scalaland.chimney.*
import io.scalaland.chimney.dsl.TransformerDefinitionCommons
import io.scalaland.chimney.{integrations, partial, PartialTransformer, Patcher, Transformer}

import scala.collection.compat.Factory
import scala.quoted

private[compiletime] trait ChimneyExprsPlatform extends ChimneyExprs { this: ChimneyDefinitionsPlatform =>
private[compiletime] object ChimneyExprsPlatform {

/**Transformer class introduced to avoid multiple anonymous classes creation*/
class TransformerClass[From, To](impl: From => To) extends Transformer[From, To] {
def transform(src: From): To = impl(src)
}
}
private[compiletime] trait ChimneyExprsPlatform extends ChimneyExprs {
this: ChimneyDefinitionsPlatform =>

object ChimneyExpr extends ChimneyExprModule {

import Expr.platformSpecific.resetOwner
import io.scalaland.chimney.internal.compiletime.ChimneyExprsPlatform.TransformerClass

object Transformer extends TransformerModule {

Expand All @@ -21,14 +30,15 @@ private[compiletime] trait ChimneyExprsPlatform extends ChimneyExprs { this: Chi

def instance[From: Type, To: Type](toExpr: Expr[From] => Expr[To]): Expr[Transformer[From, To]] =
'{
new Transformer[From, To] {
def transform(src: From): To = ${
new TransformerClass[From, To](src =>
${
PrependDefinitionsTo
.prependVal[From](resetOwner('{ src }), ExprPromise.NameGenerationStrategy.FromType)
.use(toExpr)
}
}
)
}

}

object PartialTransformer extends PartialTransformerModule {
Expand Down