OLD | NEW |
(Empty) | |
| 1 # Feature: Generic Method Syntax |
| 2 |
| 3 **This document** is an informal specification of the support in Dart 1.x |
| 4 for generic methods and functions which includes syntax and name |
| 5 resolution, but not reification of type arguments. |
| 6 |
| 7 The **motivation for** having this **feature** is that it enables partial |
| 8 support for generic methods and functions, thus providing a bridge between |
| 9 not having generic methods and having full support for generic methods. In |
| 10 particular, code declaring and using generic methods may be type checked and |
| 11 compiled in strong mode, and the same code will now be acceptable in |
| 12 standard (non-strong) mode as well. The semantics is different in certain |
| 13 cases, but standard mode analysis will emit diagnostic messages (e.g., |
| 14 errors) for that. |
| 15 |
| 16 In this document, the word **routine** will be used when referring to |
| 17 an entity which can be a non-operator method declaration, a top level |
| 18 function declaration, a local function declaration, or a function literal |
| 19 expression. Depending on the context, the word routine may also denote the |
| 20 semantic entity associated with such a declaration, e.g., a closure |
| 21 corresponding to a function literal. |
| 22 |
| 23 With **this feature** it is possible to compile code where generic methods |
| 24 and functions are declared, implemented, and invoked. The runtime semantics |
| 25 does not include reification of type arguments. Usages of the runtime |
| 26 value of a routine type parameter is a runtime error or yields `dynamic`, |
| 27 depending on the context. No type checking takes place at usages of a method |
| 28 or function type parameter in the body, and no type checking regarding |
| 29 explicitly specified or omitted type arguments takes place at call sites. |
| 30 |
| 31 In short, generic methods and functions are supported syntactically, and the |
| 32 runtime semantics prevents dynamic usages of the type argument values, but |
| 33 it allows all usages where that dynamic value is not required. For instance, |
| 34 a generic routine type parameter, `T`, cannot be used in an expression like |
| 35 `x is T`, but it can be used as a type annotation. In a context where other |
| 36 tools may perform type checking, this allows for a similar level of |
| 37 expressive power as do language designs where type arguments are erased at |
| 38 compile time. |
| 39 |
| 40 The **motivation for** this **document** is that it serves as an informal |
| 41 specification for the implementation of support for the generic method |
| 42 syntax feature in all Dart tools. |
| 43 |
| 44 ## Syntax |
| 45 |
| 46 The syntactic elements which are added or modified in order to support this |
| 47 feature are as follows, based on grammar rules given in the Dart Language |
| 48 Specification (Aug 19, 2015). |
| 49 |
| 50 ``` |
| 51 formalParameterPart: |
| 52 typeParameters? formalParameterList |
| 53 functionSignature: |
| 54 metadata returnType? identifier formalParameterPart |
| 55 typeParameter: |
| 56 metadata identifier ('extends' type)? |
| 57 functionExpression: |
| 58 formalParameterPart functionBody |
| 59 fieldFormalParameter: |
| 60 metadata finalConstVarOrType? 'this' '.' identifier |
| 61 formalParameterPart? |
| 62 argumentPart: |
| 63 typeArguments? arguments |
| 64 selector: |
| 65 assignableSelector | argumentPart |
| 66 assignableExpression: |
| 67 primary (argumentPart* assignableSelector)+ | |
| 68 'super' unconditionalAssignableSelector | |
| 69 identifier |
| 70 cascadeSection: |
| 71 '..' (cascadeSelector argumentPart*) |
| 72 (assignableSelector argumentPart*)* |
| 73 (assignmentOperator expressionWithoutCascade)? |
| 74 ``` |
| 75 |
| 76 In a [draft specification](https://codereview.chromium.org/1177073002) of |
| 77 generic methods from June 2015, the number of grammar changes is |
| 78 significantly higher, but that form can be obtained via renaming. |
| 79 |
| 80 This extension to the grammar gives rise to an **ambiguity** where the |
| 81 same tokens may be angle brackets of a type argument list as well as |
| 82 relational operators. For instance, `foo(a<b,c>(d))`[^1] may be parsed as |
| 83 a `postfixExpression` on the form `primary arguments` where the arguments |
| 84 are two relational expressions (`a<b` and `c>(d)`), and it may also be |
| 85 parsed such that there is a single argument which is an invocation of a |
| 86 generic function (`a<b,c>(d)`). The ambiguity is resolved in **favor** of |
| 87 the latter. |
| 88 |
| 89 *This is a breaking change, because existing code could include |
| 90 expressions like `foo(a < b, c > (d))` where `foo` receives two |
| 91 arguments. That expression will now be parsed as an invocation of `foo` |
| 92 with one argument. It is unlikely that this will introduce bugs silently, |
| 93 because the new parsing is likely to incur diagnostic messages at |
| 94 compile-time.* |
| 95 |
| 96 We chose to favor the generic function invocation over the |
| 97 relational expression because it is considered to be a rare exception that |
| 98 this ambiguity arises: It requires a balanced set of angle brackets followed |
| 99 by a left parenthesis, which is already an unusual form. On top of that, the |
| 100 style guide recommendation to use named parameters for boolean arguments |
| 101 helps making this situation even less common. |
| 102 |
| 103 If it does occur then there is an easy **workaround**: an extra set of |
| 104 parentheses (as in `foo(a<b,(2>(d)))`) will resolve the ambiguity in the |
| 105 direction of relational expressions; or we might simply be able to remove |
| 106 the parentheses around the last expression (as in `foo(a<b,2>d)`), which |
| 107 will also eliminate the ambiguity. |
| 108 |
| 109 _It should be noted that parsing techniques like recursive descent seem to |
| 110 conflict with this approach to disambiguation: Determining whether the |
| 111 remaining input starts with a balanced expression on the form `<` .. `>` |
| 112 seems to imply a need for unbounded lookahead. However, if some type of |
| 113 parsing is used where bracket tokens are matched up during lexical |
| 114 analysis then it takes only a simple O(1) operation in the parser to |
| 115 perform a check which will very frequently resolve the ambiguity._ |
| 116 |
| 117 ## Scope of the Mechanism |
| 118 |
| 119 With the syntax in place, it is obvious that certain potential extensions |
| 120 have **not** been **included**. |
| 121 |
| 122 For instance, constructors, setters, getters, and operators cannot be |
| 123 declared as generic: The syntax for passing actual type arguments at |
| 124 invocation sites for setters, getters, and operators is likely to be |
| 125 unwieldy and confusing, and for constructors there is a need to find |
| 126 a way to distinguish between type arguments for the new instance and |
| 127 type arguments for the constructor itself. However, there are plans |
| 128 to add support for generic constructors. |
| 129 |
| 130 This informal specification specifies a dynamic semantics where the values |
| 131 of **actual type arguments are not reified** at run time. A future |
| 132 extension of this mechanism may add this reification, such that dynamic |
| 133 type tests and type casts involving routine type variables will be |
| 134 supported. |
| 135 |
| 136 ## Resolution and Type Checking |
| 137 |
| 138 In order to be useful, the support for generic methods and functions must be |
| 139 sufficiently complete and consistent to **avoid spurious** diagnostic |
| 140 **messages**. In particular, even though no regular type checks take place |
| 141 at usages of routine type parameters in the body where they are in scope, |
| 142 those type parameters should be resolved. If they had been ignored then any |
| 143 usage of a routine type parameter `X` would give rise to a `Cannot resolve |
| 144 type X` error message, or the usage might resolve to other declarations of |
| 145 `X` in enclosing scopes such as a class type parameter, both of which is |
| 146 unacceptable. |
| 147 |
| 148 In `dart2js` resolution, the desired behavior has been achieved by adding a |
| 149 new type parameter **scope** and putting the type parameters into that |
| 150 scope, giving each of them the bound `dynamic`. The type parameter scope is |
| 151 the current scope during resolution of the routine signature and the type |
| 152 parameter bounds, it encloses the formal parameter scope of the routine, and |
| 153 the formal parameter scope in turn encloses the body scope. |
| 154 |
| 155 This implies that every usage of a routine type parameter is treated during |
| 156 **type checking** as if it had been an alias for the type dynamic. |
| 157 |
| 158 Static checks for **invocations** of methods or functions where type |
| 159 arguments are passed are omitted entirely: The type arguments are parsed, |
| 160 but no checks are applied to certify that the given routine accepts type |
| 161 arguments, and no checks are applied for bound violations. Similarly, no |
| 162 checks are performed for invocations where no type arguments are passed, |
| 163 whether or not the given routine is statically known to accept type |
| 164 arguments. |
| 165 |
| 166 Certain usages of a routine type parameter `X` give rise to **errors**: It |
| 167 is a compile-time error if `X` is used as a type literal expression (e.g., |
| 168 `foo(X)`), or in an expression on the form `e is X` or `e is! X`, or in a |
| 169 try/catch statement like `.. on T catch ..`. |
| 170 |
| 171 It could be argued that it should be a warning or an error if a routine type |
| 172 parameter `X` is used in an expression on the form `e as X`. The blind |
| 173 success of this test at runtime may introduce bugs into correct programs in |
| 174 situations where the type constraint is violated; in particular, this could |
| 175 cause "wrong" objects to propagate through local variables and parameters |
| 176 and even into data structures (say, when a `List<T>` is actually a |
| 177 `List<dynamic>`, because `T` is not present at runtime when the list is |
| 178 created). However, considering that these type constraint violations are |
| 179 expected to be rare, and considering that it is common to require that |
| 180 programs compile without warnings, we have chosen to omit this warning. A |
| 181 tool is still free to emit a hint, or in some other way indicate that there |
| 182 is an issue. |
| 183 |
| 184 ## Dynamic semantics |
| 185 |
| 186 If a routine invocation specifies actual type arguments, e.g., `int` in the |
| 187 **invocation** `f<int>(42)`, those type arguments will not be evaluated at |
| 188 runtime, and they will not be passed to the routine in the |
| 189 invocation. Similarly, no type arguments are ever passed to a generic |
| 190 routine due to call-site inference. This corresponds to the fact that the |
| 191 type arguments have no runtime representation. |
| 192 |
| 193 When the body of a generic **routine** is **executed**, usages of the formal |
| 194 type parameters will either result in a run-time error, or they will yield |
| 195 the type dynamic, following the treatment of malformed types in |
| 196 Dart. There are the following cases: |
| 197 |
| 198 When `X` is a routine type parameter, the evaluation of `e is X`, `e is! X`, |
| 199 and `X` used as an expression proceeds as if `X` had been a malformed type, |
| 200 producing a dynamic error; the evaluation of `e as X` has the same outcome |
| 201 as the evaluation of `e`. |
| 202 |
| 203 Note that the forms containing `is` are compile-time errors, which means |
| 204 that compilers may reject the program or offer ways to compile the program |
| 205 with a different runtime semantics for these expressions. The rationale for |
| 206 `dart2js` allowing the construct and compiling it to a run time error is |
| 207 that (1) this allows more programs using generic methods to be compiled, |
| 208 and (2) an `is` expression that blindly returns `true` every time (or |
| 209 `false` every time) may silently introduce a bug into an otherwise correct |
| 210 program, so the expression must fail if it is ever evaluated. |
| 211 |
| 212 When `X` is a routine type parameter which is passed as a type argument to a |
| 213 generic class instantiation `G`, it is again treated like a malformed type, |
| 214 i.e., it is considered to denote the type dynamic. |
| 215 |
| 216 This may be surprising, so let us consider a couple of examples: When `X` is |
| 217 a routine type parameter, `42 is X` raises a dynamic error, `<int>[42] is |
| 218 List<X>` yields the value `true`, and `42 as X` yields `42`, no matter |
| 219 whether the syntax for the invocation of the routine included an actual type |
| 220 argument, and, if so, no matter which value the actual type argument would |
| 221 have had at the invocation. |
| 222 |
| 223 Object construction is similar: When `X` is a routine type parameter which |
| 224 is a passed as a type argument in a constructor invocation, the actual |
| 225 value of the type type argument will be the type dynamic, as it would have |
| 226 been with a malformed type. |
| 227 |
| 228 In **checked mode**, when `X` is a routine type parameter, no checked mode |
| 229 checks will ever fail for initialization or assignment to a local variable |
| 230 or parameter whose type annotation is `X`, and if the type annotation is a |
| 231 generic type `G` that contains `X`, checked mode checks will succeed or |
| 232 fail as if `X` had been the type dynamic. Note that this differs from the |
| 233 treatment of malformed types. |
| 234 |
| 235 ## Changes |
| 236 |
| 237 2017-Jan-04: Changed 'static error' to 'compile-time error', which is the |
| 238 phrase that the language specification uses. |
| 239 |
| 240 ## Notes |
| 241 |
| 242 [^1]: These expressions violate the common style in Dart with respect to |
| 243 spacing and capitalization. That is because the ambiguity implies |
| 244 conflicting requirements, and we do not want to bias the appearance in |
| 245 one of the two directions. |
OLD | NEW |