File tree 6 files changed +42
-81
lines changed
6 files changed +42
-81
lines changed Original file line number Diff line number Diff line change @@ -51,18 +51,4 @@ object DottyPredef {
51
51
def [T ] (x : T | Null ) nn : x.type & T =
52
52
if (x == null ) throw new NullPointerException (" tried to cast away nullability, but value is null" )
53
53
else x.asInstanceOf [x.type & T ]
54
-
55
- /** Reference equality where the receiver is a nullable union.
56
- * Note that if the receiver `r` is a reference type (e.g. `String`), then `r.eq` will invoke the
57
- * `eq` method in `AnyRef`.
58
- */
59
- def (x : AnyRef | Null ) eq(y : AnyRef | Null ): Boolean =
60
- x.asInstanceOf [AnyRef ] eq y.asInstanceOf [AnyRef ]
61
-
62
- /** Reference disequality where the receiver is a nullable union.
63
- * Note that if the receiver `r` is a reference type (e.g. `String`), then `r.ne` will invoke the
64
- * `ne` method in `AnyRef`.
65
- */
66
- def (x : AnyRef | Null ) ne(y : AnyRef | Null ): Boolean =
67
- x.asInstanceOf [AnyRef ] ne y.asInstanceOf [AnyRef ]
68
54
}
Original file line number Diff line number Diff line change @@ -3,24 +3,31 @@ class Foo {
3
3
// Null itself
4
4
val x0 : Null = null
5
5
x0 == null
6
+ x0 != null
6
7
null == x0
7
8
null == null
8
9
9
10
// Nullable types: OK
10
11
val x1 : String | Null = null
11
12
x1 == null
12
13
null == x1
14
+ x1 == x0
15
+ x1 != x0
16
+ x0 == x1
13
17
14
18
// Reference types, even non-nullable ones: OK.
15
19
// Allowed as an escape hatch.
16
20
val x2 : String = " hello"
17
- x2 != null
21
+ x2 != null
18
22
x2 == null
19
23
null == x2
20
24
21
25
// Value types: not allowed.
22
- 1 == null // error
23
- null == 1 // error
24
- true == null // error
25
- null == true // error
26
+ 1 == null // error
27
+ null != 0 // error
28
+ null == 0 // error
29
+ true == null // error
30
+ null == false // error
31
+ 'a' == null // error
32
+ null == 'b' // error
26
33
}
Original file line number Diff line number Diff line change 1
-
2
1
// Test that we can't compare for equality `null` and
3
2
// classes that derive from AnyVal.
4
3
class Foo (x : Int ) extends AnyVal
5
4
6
5
class Bar {
7
6
val foo : Foo = new Foo (15 )
8
- if (foo == null ) {} // error
7
+ if (foo == null ) {} // error: Values of types Null and Foo cannot be compared
9
8
if (null == foo) {} // error
9
+ if (foo != null ) {} // error
10
+ if (null != foo) {} // error
10
11
11
12
// To test against null, make the type nullable.
12
13
val foo2 : Foo | Null = foo
13
- if (foo2 == null ) {}
14
+ if (foo2 == null ) {}
14
15
if (null == foo2) {}
16
+ if (foo2 != null ) {}
17
+ if (null != foo2) {}
15
18
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+
2
+ object Test {
3
+
4
+ def main (args : Array [String ]): Unit = {
5
+ val x : String | Null = " "
6
+ val y : String | Null = null
7
+ val z : String = " "
8
+
9
+ assert(x == x)
10
+ assert(x == " " )
11
+ assert(" " == x)
12
+ assert(x == z)
13
+ assert(z == x)
14
+ assert(x != " xx" )
15
+ assert(x != y)
16
+ assert(y == y)
17
+
18
+ assert(x != null )
19
+ assert(null != x)
20
+ assert(y == null )
21
+ assert(null == y)
22
+ assert(null == null )
23
+ }
24
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments