Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, 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 library generic_methods_local_variable_declaration_test; | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 abstract class Generator<T> { | |
| 10 T generate(); | |
| 11 } | |
| 12 | |
| 13 class A implements Generator<A> { | |
| 14 @override | |
|
karlklose
2017/03/09 09:17:00
Consider leaving out '@override'.
floitsch
2017/03/09 11:27:17
Also note that not writing any type here would inf
Dmitry Stefantsov
2017/03/10 13:21:54
Thanks!
Dmitry Stefantsov
2017/03/10 13:21:54
Done.
| |
| 15 A generate() { | |
| 16 return new A(); | |
| 17 } | |
| 18 | |
| 19 @override | |
| 20 String toString() => "instance of A"; | |
| 21 } | |
| 22 | |
| 23 class B implements Generator<B> { | |
| 24 @override | |
| 25 B generate() { | |
| 26 return new B(); | |
| 27 } | |
| 28 | |
| 29 @override | |
| 30 String toString() => "instance of B"; | |
| 31 } | |
| 32 | |
| 33 String fun<T extends Generator<T>>(T t) { | |
| 34 T another = t.generate(); | |
| 35 String anotherName = "$another"; | |
| 36 | |
| 37 Expect.isTrue(another is T); | |
| 38 Expect.isTrue(anotherName is! T); // fails if T is substituted with dynamic | |
|
floitsch
2017/03/09 11:27:17
Start comments with uppercase and finish them with
eernst
2017/03/09 14:55:02
This is again a check that seems to be justified s
Dmitry Stefantsov
2017/03/10 13:21:54
That's right. It's Dart 1 specific, so I'm removin
Dmitry Stefantsov
2017/03/10 13:21:54
Done.
| |
| 39 | |
| 40 return anotherName; | |
| 41 } | |
| 42 | |
| 43 main() { | |
| 44 A a = new A(); | |
| 45 B b = new B(); | |
| 46 | |
| 47 Expect.isTrue(fun<A>(a) == "instance of A"); | |
| 48 Expect.isTrue(fun<B>(b) == "instance of B"); | |
| 49 } | |
| OLD | NEW |