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 the Transformer#apply for intuitive implicit summoning #594

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions chimney/src/main/scala/io/scalaland/chimney/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ trait Transformer[From, To] extends Transformer.AutoDerived[From, To] {
*/
object Transformer extends TransformerCompanionPlatform {

/** Access an implicit `Transformer[From, To]`.
*
* @since 1.5.0
*/
def apply[From, To](implicit t: Transformer[From, To]): Transformer[From, To] = t
Copy link
Contributor Author

@danicheg danicheg Sep 1, 2024

Choose a reason for hiding this comment

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

It's obvious that having a counterpart method for PartialTransformer would be beneficial. However, since there is already a defined method PartialTransformer#apply, adding the counterpart method leads to a rather strange issue in Scala 2 (although it works well in Scala 3). See the minimized repro in Scastie — https://scastie.scala-lang.org/danicheg/eM7sBvzAS0q66azXmvayfg/6. Unfortunately, I haven't come up with a workaround yet, so I've left it as is.


/** Creates an empty [[io.scalaland.chimney.dsl.TransformerDefinition]] that you can customize to derive
* [[io.scalaland.chimney.Transformer]].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.scalaland.chimney

class TotalTransformerSpec extends ChimneySpec {

test("implicit summon") {
implicit val stringTransformer = new Transformer[String, Int] {
override def transform(src: String): Int = src.length
}

Transformer[String, Int].transform("foo") ==> 3
}
}
Loading