4

Simple question, I have looked at this one already: Managing imports in Scalaz7, but I can't figure out how to minimally inject the right and left methods into my objects to construct instances of \/.

I did try:

import syntax.ToDataOps and other variations of To... such as syntax.ToIdOps as suggested in http://eed3si9n.com/learning-scalaz-day13.

Simple example:

import scalaz.{\/, syntax}
import // What goes here

class Test {

    def returnEitherT(h: Int): String \/ Int = {
        h right
    }
}

Thanks, Jason.

===========

I solved it by using import syntax.id._ but I'm unsure why this worked.

Community
  • 1
  • 1
JMac
  • 549
  • 1
  • 6
  • 16
  • 1
    You should make your solution the answer - I think there are SO badges for answering your own question etc – Noel M Apr 25 '13 at 10:59
  • @NoelM thanks, but in this case I'll go with Ben's answer as he understands the reason why. – JMac Apr 26 '13 at 00:51

2 Answers2

4

syntax.id contains syntax for "plain" values, i.e. it places no constraints on the type of the value.

In general, when you import syntax for an expression of the form x.op, the place to import the syntax depends on the type of x, because op needs to be a valid operation on that type.

Because \/[A, B] is universally quantified for A and B, using the syntax x.left and x.right places no constraints on the type of x. Hence, it belongs in syntax.id.

For a working understanding of what syntax is available where, it is worth looking at the source of some of the modules that make up the syntax packages. For example, contrast IdOps[A], which has syntax for any A, with FunctorOps[F[_],A], which has the requirement that F is a Functor.


I am not sure where the name id comes from exactly; perhaps it is related to the identity functor Id, which can be defined as type Id[A] = A. If you had to choose a type constraint for values usable with syntax.id, it would be that they are in Id. Being universally quantified in A, the operations can't know about the structure of a value of A, hence they can't be structure-altering operations on A.

Ben James
  • 121,135
  • 26
  • 193
  • 155
  • This might no longer be 100% correct for latest version of Scalaz. Certainly scalaz.syntax.id.right is deprecated in favour of scalaz.syntax.either.right – Caoilte Nov 21 '14 at 15:13
2

Since scalaz 7 the correct import is:

import scalaz.syntax.either._
Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64