olu*_*ies 9 generics scala type-bounds
是否可以在Scala中执行以下操作:
class MyTest {
def foo[A <: String _or_ A <: Int](p:List[A]) = {}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,类型A可以是String或Int.这可能吗?
(这里有类似的问题)
Dan*_*ral 11
你说的不太可能,但你可以使用类型类模式.例如,从这里:
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: could not find implicit value for parameter ev: Acceptable[Double]
f(1.0)
^
Run Code Online (Sandbox Code Playgroud)
编辑
如果类和对象是伴侣,则此方法有效.在REPL上,如果您在不同的行上键入每个行(即,它们之间出现"结果"),则它们不是伴侣.您可以像下面这样输入:
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
Run Code Online (Sandbox Code Playgroud)
你可以从Either类型获得一点里程.但是,Either层次结构是密封的,处理两种以上的类型变得很麻烦.
scala> implicit def string2either(s: String) = Left(s)
string2either: (s: String)Left[String,Nothing]
scala> implicit def int2either(i: Int) = Right(i)
int2either: (i: Int)Right[Nothing,Int]
scala> type SorI = Either[String, Int]
defined type alias SorI
scala> def foo(a: SorI) {a match {
| case Left(v) => println("Got a "+v)
| case Right(v) => println("Got a "+v)
| }
| }
foo: (a: SorI)Unit
scala> def bar(a: List[SorI]) {
| a foreach foo
| }
bar: (a: List[SorI])Unit
scala>
scala> foo("Hello")
Got a Hello
scala> foo(10)
Got a 10
scala> bar(List(99, "beer"))
Got a 99
Got a beer
Run Code Online (Sandbox Code Playgroud)