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 |