Hey,
I have tried an implementation for coalesce, i.e. for expression x ?: y , if x is null, return y. but when I tried x ?: null, the compiler gives error. It seems Null type cannot be supported by implicit conversion. But I have no idea why this is the case. Can anybody explain? Thanks.
scala> implicit def elvisOperator[T >: Null <: Any](alt: T) = new {
|
| def ?:[A >: T](pred: A) = if (pred == null) alt else pred
|
| }
elvisOperator: [T >: Null](alt: T)java.lang.Object{def ?:[A >: T](pred: A): A}
scala> null ?: 1
res0: Any = 1
scala> 1 ?: null
<console>:9: error: value ?: is not a member of Null
1 ?: null
^