Created
June 5, 2018 09:03
-
-
Save chalin/d54353f704d7fca84d2a553ecf58c46f to your computer and use it in GitHub Desktop.
Language Tour: Restricting the parameterized type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://www.dartlang.org/guides/language/language-tour#restricting-the-parameterized-type | |
class SomeBaseClass {} | |
// T must be SomeBaseClass or one of its descendants. | |
class Foo<T extends SomeBaseClass> { | |
String toString() => 'Foo<$T>'; | |
} | |
class Extender extends SomeBaseClass {} | |
void main() { | |
// It's OK to use SomeBaseClass or any of its subclasses inside <>. | |
var someBaseClassFoo = new Foo<SomeBaseClass>(); | |
var extenderFoo = new Foo<Extender>(); | |
// It's also OK to use no <> at all. | |
var foo = new Foo(); | |
print('$someBaseClassFoo, $extenderFoo, $foo'); | |
// Expecting: Foo<SomeBaseClass>, Foo<Extender>, Foo<dynamic> | |
// Actual: Foo<SomeBaseClass>, Foo<Extender>, Foo<SomeBaseClass> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment