File tree 1 file changed +22
-0
lines changed
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments