【发布时间】:2015-02-08 12:59:13
【问题描述】:
好吧,我知道最好不要使用空值作为设计选择,但在这种情况下我必须这样做。为什么以下不编译?
def test[T<:AnyRef](o :Option[T]) :T = o getOrElse null
Error:(19, 53) type mismatch;
found : Null(null)
required: T
Note: implicit method foreignKeyType is not applicable here because it comes after the application point and it lacks an explicit result type
def test[T<:AnyRef](o :Option[T]) :T = o getOrElse null
^
【问题讨论】:
-
你为什么不用
Option.orNull? -
这是我的第一选择,但是当它不起作用时(原因在下面的答案中给出),我试图使示例更简单。
标签: scala generics null type-parameter