scala - None and typeclasses -


i reading "learning scalaz" blog series (this part: http://eed3si9n.com/learning-scalaz/a+yes-no+typeclass.html) , trying implement truthy class option.

here typeclass came with, pretty straight forward:

implicit def optioncantruthy[a: cantruthy]: cantruthy[option[a]] = cantruthy.truthys({   case none => false   case some(x) => x.truthy }) 

the idea if have typeclass a, can use above defined typeclass option[a] , (x:option[a]).truthy == true if , if x != none , x.get.truthy == true

it seems work fine code this:

1.some.truthy assert_=== true 0.some.truthy assert_=== false none.truthy assert_=== false 

but when try define following method:

def truthyif[a: cantruthy](cond: a)(ifyes: => string)(ifno: => string): string = {   if(cond.truthy) { ifyes } else { ifno } } 

it explodes when cond argument == none following compile error:

console>:29: error: not find implicit value evidence parameter of type cantruthy[option[nothing]]               truthyif(none)(y)(n) assert_=== n 

any ideas how fix , why won't work?

to play around code, can clone repo: git@github.com:tomasherman/scalaz.git (this code in src/scala/day1.scala)

ps: feel free change question title, im not sure 'name of problem'

my guess here code can't figure out type of option based on getting none (hence option[nothing]). try typing none first code calling knows more before passing truthyif. if string, suggestion declare this:

val opt:option[string] = none 

once code can discern underlying type, i'm guessing stop complaining.


Comments