You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/cookbook.md
+27-23Lines changed: 27 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -751,9 +751,9 @@ are described in each type's section.
751
751
had a bad experience (long compilation times, poor performance) with the automatic derivation, please note that
752
752
Chimney derivation DOES NOT work the same way, so your experiences are unlikely to carry over to Chimney.
753
753
754
-
Please, read the section below, as it will explain why replacing `import io.scalaland.chimney.dsl._` with
755
-
`Transformer.derive` + `import io.scalaland.chimney.syntax._` + `import io.scalaland.chimney.auto._` (the last one only available on 1.x line)
756
-
might actually *degrade* the performance, instead of improving it.
754
+
Please read the section below: it explains why Chimney's automatic derivation does not have the usual performance
755
+
problem. On Chimney 2.x, `io.scalaland.chimney.auto._` no longer exists; if you need to restrict where automatic
756
+
derivation may happen, use the [derivation policy](#derivation-policy-restricting-where-derivation-may-happen).
757
757
758
758
In depth explanation why automatic derivation is slow (when it's slow!) and how Chimney avoided such slowdown can be
759
759
found in [*Slow-Auto, Inconvenient-Semi: escaping false dichotomy with sanely-automatic derivation*](https://mateuszkubuszok.github.io/SlowAutoInconvenientSemi/)
@@ -819,26 +819,27 @@ The last property is a reason many projects encourage the usage of semiautomatic
819
819
and many libraries provide automatic derivation as a quick and dirty way of doing things
820
820
requiring an opt-in.
821
821
822
-
Chimney's defaults for (good) historical reasons mix these 2 modes (and one more, which
823
-
will describe in a moment), but (_due to popular demand_) it also allows you to selectively
824
-
use these imports
822
+
On the 1.x line, Chimney's defaults mixed these two modes (and one more, described below), but also allowed users to
823
+
compose these imports selectively:
825
824
826
825
!!! example
827
826
828
827
```scala
829
-
import io.scalaland.chimney.auto._ // Not available on Chimney 2.+, see below
828
+
import io.scalaland.chimney.auto._ // 1.x only
830
829
import io.scalaland.chimney.inlined._
831
830
import io.scalaland.chimney.syntax._
832
831
```
833
832
834
-
instead of `io.scalaland.chimney.dsl` to achieve a similar behavior:
833
+
instead of `io.scalaland.chimney.dsl` to achieve a similar behavior. On 2.x, `syntax._` and `inlined._` remain
834
+
available, while automatic derivation is provided directly by the `Transformer`, `PartialTransformer`, and `Patcher`
835
+
companions:
835
836
836
837
- if you `import io.scalaland.chimney.syntax._` it will expose only extension
837
838
methods working with type classes (`Transformer`, `PartialTransformer` and `Patcher`),
838
839
but with no derivation
839
840
840
-
-if you`import io.scalaland.chimney.auto._`it will only provide implicit instances
841
-
generated through derivation.
841
+
-on 1.x,`import io.scalaland.chimney.auto._`provided implicit instances generated through derivation. This
842
+
separate import does not exist on 2.x.
842
843
843
844
Semiautomatic derivation was available for a long time using methods:
844
845
@@ -955,9 +956,8 @@ or there is none and macro will handle recursion internally.
955
956
This also allows to replace a bunch of anonymous instances calling one another with a single
956
957
instance - limitting the number of allocations and improving performance.
957
958
958
-
However, with `import io.scalaland.chimney.auto._` the same semantics as in other
959
-
libraries is used: `implicit def` returns `Transformer`, so if derivation with defaults
960
-
is possible it will always be triggered.
959
+
Prior to 2.0.0, `import io.scalaland.chimney.auto._` used the same semantics as automatic derivation in other
960
+
libraries: an `implicit def` returned a `Transformer`, so derivation with defaults was triggered whenever possible.
961
961
962
962
!!! important
963
963
@@ -983,7 +983,7 @@ is possible it will always be triggered.
983
983
```scala
984
984
trait TypeClass[A]
985
985
object TypeClass {
986
-
inline given derived[A]: AutoDerived[A] =
986
+
inline given derived[A]: TypeClass[A] =
987
987
${ macroUsingSimmonIgnoring[A] }
988
988
// Inside it uses:
989
989
// Expr.summonIgnoring[TypeClass[A]](
@@ -994,7 +994,7 @@ is possible it will always be triggered.
994
994
extension [A](value: A) def foo(using TypeClass[A]) = ...
995
995
```
996
996
997
-
For that reason `import io.scalaland.chimney.auto._` does not exists on Chimney 2.0.0 for Scala 3.
997
+
For that reason `import io.scalaland.chimney.auto._` does not exist on Chimney 2.0.0 for Scala 3.
998
998
999
999
And thanks to porting the solution from Scala 3.7.0 to 2.13.17, Scala 2.13 could have align the API
1000
1000
again - removing `import io.scalaland.chimney.auto._` as well in the process. (The other consequence
@@ -1008,7 +1008,7 @@ provide `implicitConflictResolution` flag.
1008
1008
1009
1009
!!! note
1010
1010
1011
-
In other words, replicating the setup where you do:
1011
+
In other words, on Chimney 1.x, replicating the setup where you do:
1012
1012
1013
1013
```scala
1014
1014
implicit val transformer: Transformer[From, To] = locally {
@@ -1030,11 +1030,10 @@ provide `implicitConflictResolution` flag.
1030
1030
1031
1031
For the reasons above the recommendations are as follows:
1032
1032
1033
-
- if you care about performance, use either inlined derivation (`.into.transform`, for a one-time-usage) or
1034
-
semi-automatic derivation with recursion handled in the macro(`.derive`/`.define.build*` + `syntax._`, without
1035
-
importing `auto._`)
1036
-
- only use `import auto._` when you want predictable behavior similar to other libraries
1037
-
(predictably bad)
1033
+
- if you care about performance, use either inlined derivation (`.into.transform`, for a one-time usage) or
1034
+
semiautomatic derivation with recursion handled in the macro (`.derive`/`.define.build*` + `syntax._`)
1035
+
- on Chimney 2.x, use the [derivation policy](#derivation-policy-restricting-where-derivation-may-happen) when you
1036
+
need to prevent ad-hoc structural derivation outside designated scopes
1038
1037
- use unit tests to ensure, that your code does what it should do
1039
1038
- use benchmarks to ensure it is reasonably fast
1040
1039
- and keep on using `import dsl._` until you have some good proof that (recursive) semi-automatic derivation is needed
@@ -1051,6 +1050,10 @@ canonical mapping is defined in one designated place rather than materialized ad
1051
1050
**derivation policy** provides this without a separate semi-automatic API: it is a compile-time switch configured
1052
1051
entirely through `-Xmacro-settings`.
1053
1052
1053
+
On Chimney 2.x this is the replacement for controlling automatic derivation through selective
1054
+
`import io.scalaland.chimney.auto._` imports. The `auto` package no longer exists; derivation is available through the
1055
+
unified type classes, and this policy decides where their macros may generate new structural transformations.
1056
+
1054
1057
The policy gates only **structural derivation** — generating new transformation code for a `case class` or a `sealed`
0 commit comments