How do I setup multiple ORed type bounds in Scala

后端 未结 4 1208
难免孤独
难免孤独 2020-12-16 18:43

Is it possible to do something like this in Scala:

class MyTest {
   def foo[A <: String _or_ A <: Int](p:List[A]) =  {} 
}

That is,

4条回答
  •  渐次进展
    2020-12-16 18:53

    Another solution is wrapper classes:

    case class IntList(l:List[Int])
    case class StringList(l:List[String])
    
    implicit def li2il(l:List[Int]) = IntList(l)
    implicit def ls2sl(l:List[String]) = StringList(l)
    
    def foo(list:IntList) =  { println("Int-List " + list.l)}
    def foo(list:StringList) =  { println("String-List " + list.l)}
    

提交回复
热议问题