File tree Expand file tree Collapse file tree 6 files changed +42
-81
lines changed Expand file tree Collapse file tree 6 files changed +42
-81
lines changed Original file line number Diff line number Diff line change @@ -51,18 +51,4 @@ object DottyPredef {
5151 def [T ] (x : T | Null ) nn : x.type & T =
5252 if (x == null ) throw new NullPointerException (" tried to cast away nullability, but value is null" )
5353 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 ]
6854}
Original file line number Diff line number Diff line change @@ -3,24 +3,31 @@ class Foo {
33 // Null itself
44 val x0 : Null = null
55 x0 == null
6+ x0 != null
67 null == x0
78 null == null
89
910 // Nullable types: OK
1011 val x1 : String | Null = null
1112 x1 == null
1213 null == x1
14+ x1 == x0
15+ x1 != x0
16+ x0 == x1
1317
1418 // Reference types, even non-nullable ones: OK.
1519 // Allowed as an escape hatch.
1620 val x2 : String = " hello"
17- x2 != null
21+ x2 != null
1822 x2 == null
1923 null == x2
2024
2125 // 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
2633}
Original file line number Diff line number Diff line change 1-
21// Test that we can't compare for equality `null` and
32// classes that derive from AnyVal.
43class Foo (x : Int ) extends AnyVal
54
65class Bar {
76 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
98 if (null == foo) {} // error
9+ if (foo != null ) {} // error
10+ if (null != foo) {} // error
1011
1112 // To test against null, make the type nullable.
1213 val foo2 : Foo | Null = foo
13- if (foo2 == null ) {}
14+ if (foo2 == null ) {}
1415 if (null == foo2) {}
16+ if (foo2 != null ) {}
17+ if (null != foo2) {}
1518}
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