Scala minimum imports required for right and left injection

A simple question, I already looked at this: Import control in Scalaz7 , but I can’t figure out how to minimally introduce right and left into my objects to create instances \/ .

I tried:

import syntax.ToDataOps and other To... options, such as syntax.ToIdOps , as suggested at http://eed3si9n.com/learning-scalaz-day13 .

A simple example:

 import scalaz.{\/, syntax} import // What goes here class Test { def returnEitherT(h: Int): String \/ Int = { h right } } 

Thanks, Jason.

============

I solved this using import syntax.id._ , but I'm not sure why this worked.

+4
source share
2 answers

syntax.id contains the syntax for "simple" values, i.e. It does not contain restrictions on the type of value.

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

Since \/[A, B] universally quantized for A and B, using the syntax x.left and x.right does not bind constraints of type x . Therefore, it belongs to syntax.id .

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


I don’t know where exactly the name id comes from; perhaps this is due to the identity functor id , which can be defined as type Id[A] = A If you needed to choose a type restriction for values ​​that can be used with syntax.id , this means that they are in id . Being universally quantified in A , operations cannot know about the structure of the value of A , therefore they cannot be operations of changing the structure to A

+4
source

Since scalaz 7 is the correct import:

 import scalaz.syntax.either._ 
+2
source

All Articles