Skip to content

Commit 992ee7b

Browse files
authored
No More Orphans Addendum: generalized pattern doesn't seem to be as good, use OrphanDefs.scala as reference
1 parent b394449 commit 992ee7b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

_posts/2019-04-25-no-more-orphans.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ us the correct type when the library is present and pass otherwise. This functio
182182
```scala
183183
class GimmeCatsFunctor[Functor[F[_]]]
184184
object GimmeCatsFunctor {
185-
implicit val gimmeCatsFunctor: GimmeCatsFunctor[cats.Functor] = new GimmeCatsFunctor[cats.Functor]
185+
implicit def gimmeCatsFunctor: GimmeCatsFunctor[cats.Functor] = new GimmeCatsFunctor[cats.Functor]
186186
}
187187
```
188188

@@ -212,7 +212,7 @@ object MyMonad {
212212

213213
private sealed trait CatsMonad[M[_[_]]]
214214
private object CatsMonad {
215-
implicit val get: CatsMonad[cats.Monad] = null
215+
implicit def get: CatsMonad[cats.Monad] = null
216216
}
217217
```
218218

@@ -245,7 +245,7 @@ trait ImpllSemigroupalSemigroupKInvariant[K[_]] extends cats.Semigroupal[K] with
245245

246246
private sealed trait CatsSemigroupalSemigroupKInvariant[F[_[_]]]
247247
private object CatsSemigroupalSemigroupKInvariant {
248-
implicit val get: CatsSemigroupalSemigroupKInvariant[ImpllSemigroupalSemigroupKInvariant] = null
248+
implicit def get: CatsSemigroupalSemigroupKInvariant[ImpllSemigroupalSemigroupKInvariant] = null
249249
}
250250
```
251251

@@ -276,7 +276,7 @@ Changing this trait to a type alias won't work either:
276276
```scala
277277
private object CatsSemigroupalSemigroupKInvariant {
278278
type ImpllSemigroupalSemigroupKInvariant[K[_]] = cats.Semigroupal[K] with cats.SemigroupK[K] with cats.Invariant[K]
279-
implicit val get: CatsSemigroupalSemigroupKInvariant[ImpllSemigroupalSemigroupKInvariant] = null
279+
implicit def get: CatsSemigroupalSemigroupKInvariant[ImpllSemigroupalSemigroupKInvariant] = null
280280
}
281281
// [error] Symbol 'type cats.Semigroupal' is missing from the classpath.
282282
// [error] This symbol is required by 'type mylib.CatsSemigroupalSemigroupKInvariant.ImpllSemigroupalSemigroupKInvariant'.
@@ -349,7 +349,7 @@ Extracting a pattern
349349
It may be tedious to create a new class for each foreign type we want to declare optional instances for, we can extract
350350
the pattern into reusable pieces and get rid of `asInstanceOf` calls in the process by carefully crafting equality evidence.
351351
You may find one possible [implementation](https://github.com/7mind/no-more-orphans/blob/master/mylib/src/main/scala/mylib/pattern/GetTc.scala)
352-
of this pattern in the [companion repository](https://github.com/7mind/no-more-orphans) for this blog post. The repository
352+
of this pattern in the [companion repository](https://github.com/7mind/no-more-orphans) for this blog post. (ADDENDUM: "generalized" version of the pattern in GetTc.scala does not work as reliably as the raw pattern documented above and is not portable to Scala 3. The current best reference for this pattern is as it's currently used in izumi project: [OrphanDefs.scala](https://github.com/7mind/izumi/blob/9d3eedfcbaf29bdad4565d0745f746ab7b816be9/fundamentals/fundamentals-orphans/src/main/scala/izumi/fundamentals/orphans/OrphanDefs.scala)) The repository
353353
also hosts the final versions of [`MyBox`](https://github.com/7mind/no-more-orphans/blob/master/mylib/src/main/scala/mylib/MyBox.scala)
354354
and [`MyMonad`](https://github.com/7mind/no-more-orphans/blob/master/mylib/src/main/scala/mylib/MyMonad.scala) and a test suite showcasing correct implementation of the pattern.
355355

0 commit comments

Comments
 (0)