generics - How do I setup multiple ORed type bounds in Scala -
generics - How do I setup multiple ORed type bounds in Scala -
is possible in scala:
class mytest { def foo[a <: string _or_ <: int](p:list[a]) = {} } that is, type a string or int. possible?
(similar question here)
not possible set it, can using type class pattern. example, here:
sealed abstract class acceptable[t] object acceptable { implicit object intok extends acceptable[int] implicit object longok extends acceptable[long] } def f[t: acceptable](t: t) = t scala> f(1) res0: int = 1 scala> f(1l) res1: long = 1 scala> f(1.0) <console>:8: error: not find implicit value parameter ev: acceptable[double] f(1.0) ^ edit
this works if class , object companions. on repl, if type each on different line (ie, "result" appears between them), not companions. can type below, though:
scala> sealed abstract class acceptable[t]; object acceptable { | implicit object intok extends acceptable[int] | implicit object longok extends acceptable[long] | } defined class acceptable defined module acceptable scala generics
Comments
Post a Comment