Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Unified Diff: doc/GENERIC_METHODS.md

Issue 1716593002: Update docs to suggest replacing dynamic with a type parameter. (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: doc/GENERIC_METHODS.md
diff --git a/doc/GENERIC_METHODS.md b/doc/GENERIC_METHODS.md
index 833f54b220d07c57538002c938a2f5d0d1b24a1f..4a8c23f171b841d552269b94bb7a71b6de41ebee 100644
--- a/doc/GENERIC_METHODS.md
+++ b/doc/GENERIC_METHODS.md
@@ -123,6 +123,33 @@ void foo/*<S>*/(/*=S*/ x) {
}
```
+You can use the `/*=T*/` syntax to replace any type with a generic type
+parameter, but you will usually want to replace `dynamic`. Otherwise, since the
+original type is used at runtime, it may cause checked mode errors:
+
+```dart
+List/*<T>*/ makeList/*<T extends num>*/() {
+ return new List<num /*=T*/>();
+}
+
+void main() {
+ List<int> list = makeList/*<int>*/(); // <-- Fails here.
+}
+```
+
+This program checks without error in strong mode but fails at runtime in checked
+mode since the list that gets created is a `List<num>`. A better choice is:
+
+```dart
+List/*<T>*/ makeList/*<T extends num>*/() {
+ return new List/*<T>*/();
+}
+
+void main() {
+ List<int> list = makeList/*<int>*/();
+}
+```
+
## Instantiating generic classes with generic method parameters
You can use generic method parameters to instantiate generic classes using the
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698