Managing imports in Scalaz7
Asked Answered
I

1

12

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:

  • 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.

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._

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?

Incommode answered 13/11, 2012 at 9:27 Comment(3)
Did you see this already: eed3si9n.com/learning-scalaz-day13 ?Sustentation
No, but it seems exactly what I was looking for! :-)Incommode
@Sustentation If you post it as an answer, I will be glad to accept it. (actually to comply with StackExchange policies, it would be nice if you quickly summarized the content)Incommode
S
9

This blog post explains the package structure and imports a la carte in scalaz7 in detail: http://eed3si9n.com/learning-scalaz-day13

For your specific examples, for 3.failure[String] you'd need:

import scalaz.syntax.validation._

Validation already has a method 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]
Sustentation answered 13/11, 2012 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.