在 Scalaz7 中管理导入 [英] Managing imports in Scalaz7
问题描述
我在一个项目中使用 scalaz7,有时我会遇到导入问题.最简单的入门方法是
I am using scalaz7 in a project and sometimes I run into issues with imports. The simplest way get started is
import scalaz._
import Scalaz._
但有时这会导致冲突.直到现在我一直在做的以下有点痛苦的过程:
but sometimes this can lead to conflicts. What I have been doing until now the following slightly painful process:
- 找出一个需要与我的实际代码相同的导入的最小示例
- 将该示例复制到单独的项目中
- 使用选项
-Xprint:typer
编译它以找出隐式解析后代码的外观 - 在原始项目中导入所需的隐式.
- work out a minimal example that needs the same imports as my actual code
- copy that example in a separate project
- compile it with the option
-Xprint:typer
to find out how the code looks after implicit resolution - import the needed implicits in the original project.
虽然这有效,但我想简化它.我看到 scalaz7 有更细粒度的导入,但我不完全理解它们是如何组织的.例如,我看到一个可以做
Although this works, I would like to streamline it. I see that scalaz7 has much more fine-grained imports, but I do not fully understand how they are organized. For instance, I see one can do
import scalaz.std.option._
import scalaz.std.AllInstances._
import scalaz.std.AllFunctions._
import scalaz.syntax.monad._
import scalaz.syntax.all._
import scalaz.syntax.std.boolean._
import scalaz.syntax.std.all._
等等.
这些子导入是如何组织的?
How are these sub-imports organized?
举个例子,假设我想使用验证.我需要什么,例如注入验证隐式并进行以下编译?
As an example, say I want to work with validations. What would I need, for instance to inject validation implicits and make the following compile?
3.fail[String]
如何使 ValidationNEL[A, B]
成为 Applicative
的实例?
What about making ValidationNEL[A, B]
an instance of Applicative
?
推荐答案
这篇博文详细解释了 scalaz7 中的包结构和导入点菜:http://eed3si9n.com/learning-scalaz-day13
This blog post explains the package structure and imports a la carte in scalaz7 in detail: http://eed3si9n.com/learning-scalaz-day13
对于您的具体示例,对于 3.failure[String],您需要:
For your specific examples, for 3.failure[String] you'd need:
import scalaz.syntax.validation._
验证已经有一个方法ap
:
scala> "hello".successNel[Int] ap ((s: String) => "x"+s).successNel[Int]
res1: scalaz.Validation[scalaz.NonEmptyList[Int],java.lang.String] = Success(xhello)
要获得 <*> 运算符,您需要导入:
To get the <*> operator, you need this import:
import scalaz.syntax.applicative._
然后你可以这样做:
"hello".successNel[Int] <*> ((s: String) => "x"+s).successNel[Int]
这篇关于在 Scalaz7 中管理导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!