OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Verify semantics of the ?. operator when it is used to invoke a method. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 import "conditional_access_helper.dart" as h; |
| 9 |
| 10 bad() { |
| 11 Expect.fail('Should not be executed'); |
| 12 } |
| 13 |
| 14 noMethod(e) => e is NoSuchMethodError; |
| 15 |
| 16 class B {} |
| 17 |
| 18 class C extends B { |
| 19 f(callback()) => callback(); |
| 20 int g(int callback()) => callback(); |
| 21 static staticF(callback()) => callback(); |
| 22 static int staticG(int callback()) => callback(); |
| 23 } |
| 24 |
| 25 C nullC() => null; |
| 26 |
| 27 main() { |
| 28 // Make sure the "none" test fails if method invocation using "?." is not |
| 29 // implemented. This makes status files easier to maintain. |
| 30 nullC()?.f(null); |
| 31 |
| 32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). |
| 33 Expect.equals(null, nullC()?.f(bad())); /// 01: ok |
| 34 Expect.equals(1, new C()?.f(() => 1)); /// 02: ok |
| 35 |
| 36 // C?.m(...) is equivalent to C.m(...). |
| 37 Expect.equals(1, C?.staticF(() => 1)); /// 14: ok |
| 38 Expect.equals(1, h.C?.staticF(() => 1)); /// 15: ok |
| 39 |
| 40 // The static type of o?.m(...) is the same as the static type of |
| 41 // o.m(...). |
| 42 { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok |
| 43 { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok |
| 44 { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type
warning |
| 45 { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static
type warning |
| 46 { int i = C?.staticG(() => 1); Expect.equals(1, i); } /// 16: ok |
| 47 { int i = h.C?.staticG(() => 1); Expect.equals(1, i); } /// 17: ok |
| 48 { String s = C?.staticG(() => null); Expect.equals(null, s); } /// 18: static
type warning |
| 49 { String s = h.C?.staticG(() => null); Expect.equals(null, s); } /// 19: stati
c type warning |
| 50 |
| 51 // Let T be the static type of o and let y be a fresh variable of type T. |
| 52 // Exactly the same static warnings that would be caused by y.m(...) are also |
| 53 // generated in the case of o?.m(...). |
| 54 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warnin
g |
| 55 { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning |
| 56 |
| 57 // '?.' can't be used to access toplevel functions in libraries imported via |
| 58 // prefix. |
| 59 h?.topLevelFunction(); /// 11: compile-time error |
| 60 |
| 61 // Nor can it be used to access the toString method on the class Type. |
| 62 Expect.throws(() => C?.toString(), noMethod); /// 12: static type warning |
| 63 Expect.throws(() => h.C?.toString(), noMethod); /// 13: static type warning |
| 64 } |
OLD | NEW |