Cats: Add auto-widen/auto-narrow implicits

Created on 27 May 2020  路  3Comments  路  Source: typelevel/cats

It can be quite frustrating sometimes to insert widen or narrow calls sometimes just to make the compiler happy, so we thought it could be a good idea to have two little implicits you could import:

implicit def widenFunctor[F[_]: Functor, A, B >: A](fa: F[A]): F[B] = fa.widen

implicit def narrowContravariant[F[_]: Contravariant, A, B <: A](fa: F[A]): F[B] = fa.narrow

Most helpful comment

Add it to something like cats.implicits.variance._ or something like that in 2.2.0. Then take a survey and see how people like it before 2.3.0, when we could move it to the global.

All 3 comments

Might be good to try this in a few downstream projects and confirm this doesn't break something.

Add it to something like cats.implicits.variance._ or something like that in 2.2.0. Then take a survey and see how people like it before 2.3.0, when we could move it to the global.

So as a random datapoint鈥β營 added these implicits to every scope in https://github.com/precog/quasar. Everything compiled just fine before and after. There was a 12% performance hit (which is actually a bit less than I was expecting) in compile time after everything was warmed up. To be clear, quasar makes very aggressive use of implicit search across a large section of the codebase, as well as some very complicated type inference, so it probably represents a worse case scenario in terms of compiler performance impact of this change.

Was this page helpful?
0 / 5 - 0 ratings