Implicit parameter in Scalaz

I'm trying to find out why the call works in works scalaz.ListW.<^>

def <^>[B: Zero](f: NonEmptyList[A] => B): B = value match {
  case Nil => ∅
  case h :: t => f(Scalaz.nel(h, t))
}

My minimal theory:

trait X[T]{
   def y : T
}

object X{
  implicit object IntX extends X[Int]{
    def y = 42 
  }
  implicit object StringX extends X[String]{
    def y = "y" 
  } 
}
trait Xs{
  def ys[T](implicit x : X[T]) = x.y 
}

class A extends Xs{
  def z[B](implicit x : X[B]) : B = ys //the call ∅
}

What produces:

import X._

scala> new A().z[Int]
res0: Int = 42

scala> new A().z[String]
res1: String = y

It's really? Is it possible to achieve the same result with a smaller step?

+5
source share
1 answer

That's all. You can remove Xsand save the essence of the example:

object A{
  def ys[T](implicit x : X[T]) = x.y 
}
A.ys

Another interesting aspect of use in Scalaz is that the type argument is inferred from the expected type of the Bexpression.

Zero . ; . , , , .

+1

All Articles