简体   繁体   English

在Scalaz中管理进口

[英]Managing imports in Scalaz7

I am using scalaz7 in a project and sometimes I run into issues with imports. 我在项目中使用scalaz7,有时会遇到导入问题。 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: 到目前为止,我一直在做以下痛苦的过程:

  • 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 使用选项-Xprint:typer对其进行编译,以找出隐式解析后代码的外观
  • import the needed implicits in the original project. 在原始项目中导入所需的隐式。

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. 我看到scalaz7具有更多的细粒度导入,但是我不完全了解它们的组织方式。 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._

and so on. 等等。

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]

What about making ValidationNEL[A, B] an instance of Applicative ? 如何使ValidationNEL[A, B]成为Applicative的实例?

This blog post explains the package structure and imports a la carte in scalaz7 in detail: http://eed3si9n.com/learning-scalaz-day13 这篇博客文章解释了包的结构,并详细地在scalaz7中导入了点菜: http ://eed3si9n.com/learning-scalaz-day13

For your specific examples, for 3.failure[String] you'd need: 对于您的特定示例,对于3.failure [String],您需要:

import scalaz.syntax.validation._

Validation already has a method ap : 验证已经有方法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._

Then you can do: 然后,您可以执行以下操作:

"hello".successNel[Int] <*> ((s: String) => "x"+s).successNel[Int]

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

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