2010-02-25 119 views
11

我试过两种方法将泛型类型参数约束为可空类型,但两者似乎都有一些意想不到的问题。Scala对可为空类型的泛型约束如何工作

第一次尝试(使用T- <:AnyRef):

scala> def testAnyRefConstraint[T <: AnyRef](option:Option[T]):T = { 
    | //without the cast, fails with compiler error: 
    | // "found: Null(null) required: T" 
    | option getOrElse null.asInstanceOf[T] 
    | } 
testAnyRefConstraint: [T <: AnyRef](Option[T])T 

scala> testAnyRefConstraint(Some("")) 
res0: java.lang.String = 

scala> testAnyRefConstraint(Some(0)) 
<console>:16: error: inferred type arguments [Int] do not conform to method testAnyRefConstraint's type parameter bounds [T <: AnyRef] 
     testAnyRefConstraint(Some(0)) 

这似乎做的正是我想要的,但我不明白为什么空需要被转换为T.


第二次尝试(使用T>:空):

scala> def testNullConstraint[T >: Null](option:Option[T]):T = { 
    | option getOrElse null 
    | } 
testNullConstraint: [T >: Null](Option[T])T 

scala> testNullConstraint(Some("")) 
res2: java.lang.String = 

scala> testNullConstraint(Some(0)) 
res3: Any = 0 

这并不需要对空的投,但它允许AnyVals被传递并将类型转换为任何类型,这不是我正在寻找的。

有没有人有任何想法,为什么这两种不同的方法工作,他们这样做?

回答

20
def testAnyRefConstraint[T >: Null <: AnyRef](option:Option[T]):T = { 
    option getOrElse null 
} 

当我第一次犯这个错误时,我觉得真的很愚蠢。仅仅因为延伸AnyRef并不意味着它必须是空的。例如,NothingAnyRef的子类型,它不是可空的。

其他的方法是相似的,因为Any是超类型Null,并且任何Int也是Any