Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 // Dart test program for constructors and initializers. | |
| 5 | |
| 6 // Test 'expression as Type' casts. | |
| 7 | |
| 8 class C { | |
| 9 final int foo = 42; | |
| 10 } | |
| 11 | |
| 12 class D extends C { | |
| 13 final int bar = 37; | |
| 14 } | |
| 15 | |
| 16 Object createC() => new C(); | |
| 17 Object createD() => new D(); | |
| 18 Object getNull() => null; | |
| 19 Object createList() => <int>[2]; | |
| 20 Object createInt() => 87; | |
| 21 Object createString() => "a string"; | |
| 22 | |
| 23 main() { | |
| 24 Object oc = createC(); | |
| 25 Object od = createD(); | |
| 26 Object on = getNull(); | |
| 27 Object ol = createList(); | |
| 28 Object oi = createInt(); | |
| 29 Object os = createString(); | |
| 30 | |
| 31 Expect.equals(42, (oc as C).foo); | |
| 32 Expect.equals(42, (od as C).foo); | |
| 33 Expect.equals(42, (od as D).foo); | |
| 34 Expect.equals(37, (od as D).bar); | |
| 35 Expect.equals(37, ((od as C) as D).bar); | |
| 36 (oc as D).foo; /// 01: runtime error | |
| 37 (on as D).foo; /// 02: runtime error | |
| 38 (on as C).foo; /// 03: runtime error | |
| 39 oc.foo; /// 04: static type warning | |
| 40 od.foo; /// 05: static type warning | |
| 41 on.foo; /// 06: runtime error | |
| 42 (on as Object).toString(); | |
| 43 (oc as Object).toString(); | |
| 44 (od as Object).toString(); | |
| 45 (on as Dynamic).foo; /// 07: runtime error | |
| 46 (oc as Dynamic).foo; | |
| 47 (od as Dynamic).foo; | |
| 48 (oc as Dynamic).bar; /// 08: runtime error | |
| 49 (od as Dynamic).bar; | |
| 50 C c = oc as C; | |
| 51 c = od as C; | |
| 52 c = oc; // 09: static type warning | |
|
ahe
2012/06/07 13:12:59
Use three slashes. Some problem elsewhere in this
| |
| 53 D d = od as D; | |
| 54 d = oc as D; /// 10: runtime error | |
| 55 d = od; // 11: static type warning | |
| 56 | |
| 57 (ol as List)[0]; | |
| 58 (ol as List<int>)[0]; | |
| 59 (ol as Dynamic)[0]; | |
| 60 (ol as String).length; /// 12: runtime error | |
| 61 int x = (ol as List<int>)[0]; | |
| 62 (ol as List<int>)[0] = (oi as int); | |
| 63 | |
| 64 (os as String).length; | |
| 65 (os as Dynamic).length; | |
| 66 (oi as String).length; /// 13: runtime error | |
| 67 (os as List).length; /// 14: runtime error | |
| 68 | |
| 69 (oi as int) + 2; | |
| 70 (oi as List).length; /// 15: runtime error | |
| 71 } | |
| 72 | |
| 73 | |
| OLD | NEW |