In scalaz when we define a module, we additionally define implicit, helper functions. Here is an example of definition and how it could be used by a client:
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
object Functor {
def fmap[F[_], A,B](as:F[A])(f:A=>B)
(implicit ff:Functor[F]):F[B] =
ff.map(as)(f)
implicit val listFunctor = new Functor[List] {
def map[A,B](as: List[A])(f: A => B): List[B] = as map f
}
}
...
import com.savdev.NewLibrary._
val r = fmap(List(1,2))(_.toString)
final class FunctorOps[F[_], A](self: F[A])(implicit ff:Functor[F]){
def qmap[B](f:A=>B):F[B] = ff.map(self)(f)
}
trait ToFunctorOps {
implicit def ToFunctorOps[F[_],A](v: F[A])(implicit F0: Functor[F]) =
new FunctorOps[F,A](v)
}
object NewLibrary extends ToFunctorOps
...
import com.savdev.NewLibrary._
val r2 = List(1, 4) qmap (x=>x.toString)
The code is slightly changed. But the idea is that we define:
- An abstraction and its API (algebra)
- Define helper generic functions that use implicits and implicits themselves
- Enrich existing types to be able to use our new abstraction. Implicit convertion is used for that. In scalaz we define a final class for a wrapper and implicit converters in traits
All above, the motivation of it and how it can be used by a client is clear. But in scalaz
to each such module definition, there is also a related *Syntax
class. I cannot understand the purpose of it. Can you please exlain, why it is needed and HOW it can be used in a client code.
In Scalaz it is defined as:
trait FunctorSyntax[F[_]] {
implicit def ToFunctorOps[A](v: F[A]): FunctorOps[F, A] =
new FunctorOps[F, A](v)(FunctorSyntax.this.F)
def F: Functor[F]
}
UPDATED:
Guys, it seems I am not clear enough, or a topic is more complicated for all of us.
What I need is to understand the difference between two traits:
trait ToFunctorOps {
implicit def ToFunctorOps[F[_],A](v: F[A])(implicit F0: Functor[F]) =
new FunctorOps[F,A](v)
}
vs.
trait FunctorSyntax[F[_]] {
implicit def ToFunctorOps[A](v: F[A]): FunctorOps[F, A] =
new FunctorOps[F, A](v)(FunctorSyntax.this.F)
def F: Functor[F]
}
Both traits define a generic method that creates FunctorOps
, both have the same visibility rules.
The first ToFunctorOps
trait, it is not generic itself, it defines only generic method with [F[_],A]
. As a result, I can compose a lot of such traits into one object and import all of them at once. I gave an example how such traits could be used by a client:
object NewLibrary extends ToFunctorOps
...
import com.savdev.NewLibrary._
val r2 = List(1, 4) qmap (x=>x.toString)
This trait already gives clients a possibility to inject methods implicitly. Why do we need FunctorSyntax
? This FunctorSyntax trait is a generic itself on [F[_]]
. When I extend it, I must provide a type in the definition. Because F[_]
now is used in trait definition, a function has less generic parameters, only [A]
.
I am asking you guys, if you can help and undestand, give me a code example how this FunctorSyntax
trait can be used by a client. Exactly this is not clear.
Right now I see tries to explain the other topics, but not the original:
- How to create implicit classes, instead of implicit functions.
- Difference between final *Ops class and a trait, including their visibility. Here we compare 2 traits with the same visibility.
- Explaining in general method injection, how they help. This functionality is provided already with
ToFunctorOps
.
Guys, again, please show the community USE CASES via CODE of FunctorSyntax
. Code itself is always the best documentation.
Best regards
ToFunctorOps
definition? I only seetrait ToFunctorOps[TC[F[_]] <: Functor[F]] extends ToFunctorOps0[TC] with ToInvariantFunctorOps[TC]