Scala type bounds =:= -
while compiles:
implicit class container[t](val value:t) extends anyval{ def addint(x:int)(implicit ev:t=:=int) = value+x }
this complains type mismatch, expected t
, actual int
, if ignores type bound.
implicit class container[t](val value:t=>int) extends anyval{ def addint(x:int)(implicit ev:t=:=int) = value(x) }
why?
your type constraint backwards, actually. t =:= int
provides implicit evidence t
int
, not exactly int
t
. if @ declaration if =:=
, you'll see goes 1 way:
sealed abstract class =:=[from, to] extends (from => to) serializable
your first example works because value
t
, , constraint t =:= int
, implicitly converts t
int
. second example, need feed t
value: t => int
, need other direction.
this works:
implicit class container[t](val value: t => int) extends anyval { def addint(x: int)(implicit ev: int =:= t) = value(x) }
the reason why second example using int <:< t
works because <:<
provides implicit conversion int => t
.
Comments
Post a Comment