File tree 3 files changed +48
-3
lines changed
3 files changed +48
-3
lines changed Original file line number Diff line number Diff line change
1
+ // Test that reference types are no longer nullable.
2
+
3
+ class Foo {
4
+ val s : String = null // error
5
+ val s1 : String | Null = null
6
+ val s2 : String | Null = " "
7
+
8
+ val b : Boolean = null // error
9
+ val c : Int | Null = null
10
+
11
+ val ar1 : AnyRef = null // error
12
+ val ar2 : AnyRef | Null = null
13
+ val ob : Object = null // error
14
+
15
+ val av : AnyVal = null // error
16
+ val a : Any = null
17
+ val n : Null = null
18
+ }
Original file line number Diff line number Diff line change
1
+ class Foo {
2
+ val x : String = null // error: String is non-nullable
3
+
4
+ def foo (x : String ): String = " x"
5
+
6
+ val y = foo(null ) // error: String argument is non-nullable
7
+
8
+ val z : String = foo(" hello" )
9
+
10
+ class Bar
11
+ val b : Bar = null // error: user-created classes are also non-nullable
12
+ }
Original file line number Diff line number Diff line change 2
2
// Enabling unsafeNulls allows this kind of unsafe operations,
3
3
// but could cause exception during runtime.
4
4
5
- import scala .language .unsafeNulls
5
+ object F {
6
+ def apply (x : String ): String = x
7
+ }
8
+
9
+ object G {
10
+ def h (f : String | Null => String , x : String | Null ): String | Null =
11
+ f(x)
12
+ }
6
13
7
14
object Test {
15
+ import scala .language .unsafeNulls
16
+
8
17
def main (args : Array [String ]): Unit = {
9
- val s : String | Null = " hello"
10
- assert(s .length == 5 )
18
+ val s1 : String | Null = " hello"
19
+ assert(s1 .length == 5 )
11
20
12
21
val s2 : String | Null = null
13
22
try {
@@ -17,5 +26,11 @@ object Test {
17
26
case e : NullPointerException =>
18
27
// ok: Selecting on a null value would throw NullPointerException.
19
28
}
29
+
30
+ val s3 : String = F (s1)
31
+ assert(s3.length == 5 )
32
+
33
+ val s4 : String = G .h(F .apply, s1)
34
+ assert(s4.length == 5 )
20
35
}
21
36
}
You can’t perform that action at this time.
0 commit comments