简体   繁体   English

猫:如何从implicits中找到特定类型

[英]Cats: how to find the specific type from implicits

I have this code which compiles and works fine 我有这个代码编译和工作正常

import cats.implicits._
Cartesian[ValidResponse].product(
    getName(map).toValidated,
    readAge(map).toValidated
).map(User.tupled)

However I don't like the import of cats.implicits._ because there is just too many classes there. 但是我不喜欢cats.implicits._的导入,因为那里的课程太多了。 I tried importing specific things related to Cartesians like 我尝试导入与笛卡尔相关的特定事物

import cats.implicits.catsSyntaxCartesian
import cats.implicits.catsSyntaxUCartesian
import cats.implicits.catsSyntaxTuple2Cartesian

But these did not work. 但这些都行不通。 As a newbie I find the implicit imports very confusing because there are simply 1000s of them and the names are not very obvious. 作为一个新手,我发现隐式导入非常令人困惑,因为它们只有1000个并且名称不是很明显。 My only alternative is to import the entire universe by import cats.implicits._ and stop thinking about it. 我唯一的选择是通过import cats.implicits._导入整个宇宙,并停止思考它。

In fact I have a broader confusion about cats.implicits , cats.instances._ and cats.syntax._ . 事实上,我对cats.implicitscats.instances._cats.syntax._有更广泛的困惑。 So far I am just importing these via trial and error. 到目前为止,我只是通过反复试验导入这些。 I am not really sure of when to import what. 我不确定何时导入什么。

Do not try to pick out specific things from cats.implicits . 不要试图从cats.implicits挑选出具体的东西。 You either import the entire thing, or you don't use it at all. 您可以导入整个内容,也可以根本不使用它。 Further, there's no reason to be afraid of importing it all. 此外,没有理由害怕将其全部导入。 It can't interfere with anything. 它不会干扰任何事情。

Ok, I lied. 好的,我骗了。 It will interfere if you import cats.instances.<x>._ and/or cats.syntax.<x>._ alongside cats.implicits._ . 如果您导入cats.instances.<x>._和/或cats.syntax.<x>._以及cats.implicits._将会发生干扰。 These groups are meant to be mutually exclusive: you either import everything and forget about it with cats.implicits._ , or you specifically select what you want to import with cats.instances and cats.syntax . 这些组是互斥的:您要么导入所有内容而忘记cats.implicits._ ,要么专门选择要用cats.instancescats.syntax导入的cats.syntax

These two packages are not meant to be imported completely like cats.implicits . 这两个包并不cats.implicits那样完全cats.implicits Instead, they include a bunch of objects . 相反, 它们包括 一堆对象 Each object contains some implicit instances/syntax, and you are meant to import from those . 每个对象都包含一些隐式实例/语法,您可以从那些对象导入。

import cats.implicits._ // Good, nothing to fear
// RESET IMPORTS
import cats.implicits.catsSyntaxCartesian // Bad, don't pick and choose
// RESET IMPORTS
import cats.instances._ // Bad, is useless
import cats.syntax._    // Ditto
// RESET IMPORTS
import cats.instances.list._   // ok
import cats.syntax.cartesian._ // ok
// RESET IMPORTS
import cats.implicits._
import cats.syntax.monad._ // Bad, don't mix these two

Additionally each of cats.{ instances, syntax } contains an all object, with the obvious function. 此外,每个cats.{ instances, syntax }包含一个具有明显功能的all对象。 The import cats.implicits._ is really a shortcut for import cats.syntax.all._, cats.instances.all._ . 导入cats.implicits._实际上是import cats.syntax.all._, cats.instances.all._的快捷方式。

I'll start by saying that import cats.implicits._ is safe, reasonable and the recommended approach when starting. 我首先要说import cats.implicits._是安全,合理的,并且在启动时是推荐的方法。 So if the only reason for this question is that you don't like importing too many classes, then I think you should just bite the bulled at leave that as is. 因此,如果这个问题的唯一原因是你不喜欢导入太多的课程,那么我认为你应该只是把那个被诅咒的人留下来。

Additionally, I recommend you take a look at the official cats import guide . 另外,我建议你看看官方猫进口指南 It tries to explain the package/logical structure of cats code and might make it easier to understand. 它试图解释猫代码的包/逻辑结构,并可能使它更容易理解。

The "cats" library is organized in several "areas" that can be easily distinguished by their package name: “猫”图书馆分为几个“区域”,可以通过包名轻松区分:

  1. cats._ - This is where most of the typeclasses live (eg Monad, Foldable etc.) cats._ - 这是大多数类型生活的地方(例如Monad,Foldable等)
  2. cats.data._ - This is the home of data structures like Validated and State. cats.data._ - 这是Validated和State等数据结构的所在地。
  3. cats.instances._ - This is where the instances for the typeclasses defined in 1 are. cats.instances._ - 这是1中定义的类型类的实例。 For example if you import cats.instances.list._ you'll bring into scope the Show, Monad etc. instances for the standard List. 例如,如果导入cats.instances.list._ ,则会将标准List的Show,Monad等实例纳入范围。 This is what you're most likely interested in . 这是你最感兴趣的东西
  4. cats.syntax._ - has some syntax enrichment that makes code easier to write and read. cats.syntax._ - 有一些语法丰富,使代码更容易编写和读取。

An example of ussing cats.syntax._ would be: ussing cats.syntax._一个例子是:

import cats.Applicative
import cats.syntax.applicative._

val listOfInt = 5.pure[List]
//instead of 
val otherList = Applicative[List[Int]].pure(5)

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

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