Implicit parameter in Scalaz

Posted by Thomas Jung on Stack Overflow See other posts from Stack Overflow or by Thomas Jung
Published on 2010-04-30T18:31:24Z Indexed on 2010/05/01 13:17 UTC
Read the original article Hit count: 259

Filed under:
|
|

I try to find out why the call Ø in scalaz.ListW.<^> works

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

My minimal theory is:

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 Ø
}

Which produces:

import X._

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

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

Is this valid? Can I achieve the same result with fewer steps?

© Stack Overflow or respective owner

Related posts about scala

Related posts about scalaz