Skip to content

Instantly share code, notes, and snippets.

@chalin
Created June 5, 2018 09:03
Show Gist options
  • Save chalin/d54353f704d7fca84d2a553ecf58c46f to your computer and use it in GitHub Desktop.
Save chalin/d54353f704d7fca84d2a553ecf58c46f to your computer and use it in GitHub Desktop.
Language Tour: Restricting the parameterized type
// 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