OLD | NEW |
1 # Using Generic Methods | 1 # Using Generic Methods |
2 | 2 |
3 **Note: For historical reasons, this feature is called "generic methods", but it | 3 **Note: For historical reasons, this feature is called "generic methods", but it |
4 applies equally well to instance methods, static methods, top-level functions, | 4 applies equally well to instance methods, static methods, top-level functions, |
5 local functions, and even lambda expressions.** | 5 local functions, and even lambda expressions.** |
6 | 6 |
7 Initially a [proposal][], generic methods are on their way to being fully | 7 Initially a [proposal][], generic methods are on their way to being fully |
8 supported in Dart. Here is how to use them. | 8 supported in Dart. Here is how to use them. |
9 | 9 |
10 [proposal]: https://github.com/leafpetersen/dep-generic-methods/blob/master/prop
osal.md | 10 [proposal]: https://github.com/leafpetersen/dep-generic-methods/blob/master/prop
osal.md |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 ```dart | 103 ```dart |
104 useThing<T>(T thing) { | 104 useThing<T>(T thing) { |
105 var pair = <T>[thing, thing]; | 105 var pair = <T>[thing, thing]; |
106 // ^-- Here. | 106 // ^-- Here. |
107 var set = new Set<T>()..add(thing); | 107 var set = new Set<T>()..add(thing); |
108 // ^-- And here. | 108 // ^-- And here. |
109 } | 109 } |
110 ``` | 110 ``` |
111 | 111 |
112 Note that generic methods are not yet supported *at runtime* on the VM and | 112 Note that generic methods are not yet supported *at runtime* on the VM and |
113 dartjs. On those platforms, uses of generic method type arguments are | 113 dart2js. On those platforms, uses of generic method type arguments are |
114 treated like `dynamic` today. So in this example, `pair`'s reified type at | 114 treated like `dynamic` today. So in this example, `pair`'s reified type at |
115 runtime will be `List<dynamic>` and `set` will be `Set<dynamic>`. | 115 runtime will be `List<dynamic>` and `set` will be `Set<dynamic>`. |
116 | 116 |
117 There are two places you *cannot* use a generic method type parameter. Both are | 117 There are two places you *cannot* use a generic method type parameter. Both are |
118 because the VM and dart2js don't support reifying generic method type arguments | 118 because the VM and dart2js don't support reifying generic method type arguments |
119 yet. Since these expressions wouldn't do what you want, we've temporarily | 119 yet. Since these expressions wouldn't do what you want, we've temporarily |
120 defined them to be an error: | 120 defined them to be an error: |
121 | 121 |
122 * As the right-hand side of an `is` or `is!` expression. | 122 * As the right-hand side of an `is` or `is!` expression. |
123 | 123 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 instead. If that isn't what you want, or it infers a type you don't want, you | 179 instead. If that isn't what you want, or it infers a type you don't want, you |
180 can always pass them explicitly: | 180 can always pass them explicitly: |
181 | 181 |
182 ```dart | 182 ```dart |
183 // Explicitly give a type so that we don't infer "int". | 183 // Explicitly give a type so that we don't infer "int". |
184 var lengths = fruits.map<num>((fruit) => fruit.length).toList(); | 184 var lengths = fruits.map<num>((fruit) => fruit.length).toList(); |
185 | 185 |
186 // So that we can later add doubles to the result. | 186 // So that we can later add doubles to the result. |
187 lengths.add(1.2); | 187 lengths.add(1.2); |
188 ``` | 188 ``` |
OLD | NEW |