简体   繁体   English

为什么导入类型类实例不再需要 importcats.implicits._ ?

[英]Why is import cats.implicits._ no longer necessary for importing type class instances?

In Cats 2.1.x type class instances were brought in scope with import cats.implicits._在 Cats 2.1.x 中,类型类实例通过import cats.implicits._引入范围

scala> import cats.Show
import cats.Show

scala> Show[Int].show(42)
<console>:13: error: could not find implicit value for parameter instance: cats.Show[Int]
       Show[Int].show(42)
           ^

scala> import cats.implicits._
import cats.implicits._

scala> Show[Int].show(42)
res1: String = 42

however in Cats 2.2.0 it works without import cats.implicits._ for example但是,在 Cats 2.2.0 中,它无需import cats.implicits._ ,例如

scala> import cats.Show
import cats.Show

scala> Show[Int].show(42)
val res0: String = 42

What changed and how should we use imports from now on?发生了什么变化,从现在开始我们应该如何使用导入?

In 2.1.x instances of type classes were defined in separate objects so in order to be in scope (local scope) they had to be imported在 2.1.x 中,类型类的实例是在单独的对象中定义的,因此为了在范围内(本地范围),它们必须被导入

object implicits extends instances.AllInstances with ...

trait AllInstances extends AnyValInstances with ...

trait AnyValInstances extends IntInstances with ...

trait IntInstances extends cats.kernel.instances.IntInstances {
  implicit val catsStdShowForInt: Show[Int] = Show.fromToString[Int]
}

In 2.2.0 instances of type classes are defined in companion objects so they are in scope (implicit scope) automatically and do not have to be imported在 2.2.0 中,类型类的实例在伴生对象中定义,因此它们自动在范围内(隐式范围)并且不必导入

object Show extends ScalaVersionSpecificShowInstances with ShowInstances {
  ...
  implicit def catsShowForInt: Show[Int] = cats.instances.int.catsStdShowForInt
  ...
}

Release notes https://github.com/typelevel/cats/releases/tag/v2.2.0发行说明https://github.com/typelevel/cats/releases/tag/v2.2.0

In most cases all that's necessary to switch to using the new implicit scope instances is to replace cats.implicits._ imports with cats.syntax.all._ and delete any cats.instances imports.在大多数情况下,切换到使用新的隐式作用域实例所需要的cats.implicits._cats.syntax.all._替换cats.implicits._导入并删除任何cats.instances导入。 You don't have to make this change to use Cats 2.2.x, though, since this release doesn't remove anything.不过,您无需进行此更改即可使用 Cats 2.2.x,因为此版本不会删除任何内容。 Importing cats.implicits._ will do exactly the same thing on Cats 2.1.x and 2.2.x, since imported instances have higher priority than implicit scope.导入cats.implicits._在Cats 2.1.x 和2.2.x 上会做完全相同的事情,因为导入的实例比隐式作用域具有更高的优先级。 You just won't see improvements in compile times.你只是不会看到编译时间的改进。

There is one exception to this rule.这一规则有一个例外。 The cats.implicits package provides implicit conversions from Cats's own Order and PartialOrder type classes to the standard library's Ordering and PartialOrdering . cats.implicits包提供了从 Cats 自己的OrderPartialOrder类型类到标准库的OrderingPartialOrdering隐式转换。 This conversion is not available in implicit scope, because it would be a bad idea for Cats to put instances of type classes it doesn't own into scope for types that it doesn't own (and also because it's not possible).这种转换在隐式作用域中不可用,因为对于 Cats 来说,将它不拥有的类型类的实例放入它不拥有的类型的作用域中是一个坏主意(也因为这是不可能的)。

Where does Scala look for implicits? Scala 在哪里寻找隐式?

https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM