Skip to content

Commit 04982b1

Browse files
committed
Add nullable type params test
1 parent 2a77b33 commit 04982b1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** We used to use `T >: Null <: AnyRef` to represent a reference type,
2+
* and `T` cannot be `Nothing`. However, with explicit nulls, this definition
3+
* is no longer valid, because `Null` is not a subtype of `AnyRef`.
4+
*
5+
* For example:
6+
* ```scala
7+
* def nullOf[T >: Null <: AnyRef]: T = null
8+
* ```
9+
*
10+
* We can modify the definition as following to allow only nullable type paramters.
11+
*/
12+
13+
def nullOf[T >: Null <: AnyRef | Null]: T = null
14+
15+
def f = {
16+
val s1 = nullOf[String] // error: Type argument String does not conform to lower bound Null
17+
val s2 = nullOf[String | Null] // ok
18+
19+
val n = nullOf[Null]
20+
val i = nullOf[Int] // error: Type argument Int does not conform to upper bound AnyRef | Null
21+
val a = nullOf[Any] // error: Type argument Any does not conform to upper bound AnyRef | Null
22+
}

0 commit comments

Comments
 (0)