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

Side by Side Diff: docs/language/dartLangSpec.tex

Issue 2399343002: Change how the language specification describes control flow. (Closed)
Patch Set: Address comments from kmillikin. Created 4 years, 2 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 \documentclass{article} 1 \documentclass{article}
2 \usepackage{epsfig} 2 \usepackage{epsfig}
3 \usepackage{color} 3 \usepackage{color}
4 \usepackage{dart} 4 \usepackage{dart}
5 \usepackage{bnf} 5 \usepackage{bnf}
6 \usepackage{hyperref} 6 \usepackage{hyperref}
7 \usepackage{lmodern} 7 \usepackage{lmodern}
8 \newcommand{\code}[1]{{\sf #1}} 8 \newcommand{\code}[1]{{\sf #1}}
9 \title{Dart Programming Language Specification \\ 9 \title{Dart Programming Language Specification \\
10 {4th edition draft}\\ 10 {4th edition draft}\\
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 \LMHash{} 156 \LMHash{}
157 Dart programs may be executed in one of two modes: production mode or checked mo de. In production mode, static type annotations (\ref{staticTypes}) have absolut ely no effect on execution with the exception of reflection and structural type tests. 157 Dart programs may be executed in one of two modes: production mode or checked mo de. In production mode, static type annotations (\ref{staticTypes}) have absolut ely no effect on execution with the exception of reflection and structural type tests.
158 158
159 \commentary{ 159 \commentary{
160 Reflection, by definition, examines the program structure. If we provide reflect ive access to the type of a declaration, or to source code, it will inevitably p roduce results that depend on the types used in the underlying code. 160 Reflection, by definition, examines the program structure. If we provide reflect ive access to the type of a declaration, or to source code, it will inevitably p roduce results that depend on the types used in the underlying code.
161 161
162 Type tests also examine the types in a program explicitly. Nevertheless, in most cases, these will not depend on type annotations. The exceptions to this rule a re type tests involving function types. Function types are structural, and so de pend on the types declared for their parameters and on their return types. 162 Type tests also examine the types in a program explicitly. Nevertheless, in most cases, these will not depend on type annotations. The exceptions to this rule a re type tests involving function types. Function types are structural, and so de pend on the types declared for their parameters and on their return types.
163 } 163 }
164 164
165 \LMHash{} 165 \LMHash{}
166 In checked mode, assignments are dynamically checked, and certain violations of the type system raise exceptions at run time. 166 In checked mode, assignments are dynamically checked, and certain violations of the type system throw exceptions at run time.
167 167
168 \commentary{ 168 \commentary{
169 The coexistence between optional typing and reification is based on the followin g: 169 The coexistence between optional typing and reification is based on the followin g:
170 \begin{enumerate} 170 \begin{enumerate}
171 \item Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages). Reified type information includes cla ss declarations, the runtime type (aka class) of an object, and type arguments t o constructors. 171 \item Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages). Reified type information includes cla ss declarations, the runtime type (aka class) of an object, and type arguments t o constructors.
172 \item Static type annotations determine the types of variables and function decl arations (including methods and constructors). 172 \item Static type annotations determine the types of variables and function decl arations (including methods and constructors).
173 \item Production mode respects optional typing. Static type annotations do not a ffect runtime behavior. 173 \item Production mode respects optional typing. Static type annotations do not a ffect runtime behavior.
174 \item Checked mode utilizes static type annotations and dynamic type information aggressively yet selectively to provide early error detection during developmen t. 174 \item Checked mode utilizes static type annotations and dynamic type information aggressively yet selectively to provide early error detection during developmen t.
175 \end{enumerate} 175 \end{enumerate}
176 } 176 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 a program that interfaces between the engine and the surrounding computing envir onment. The embedder will often be a web browser, but need not be; it may be a C ++ program on the server for example. When an isolate fails with a compile-time error as described above, control returns to the embedder, along with an excepti on describing the problem. This is necessary so that the embedder can clean up resources etc. It is then the embedder's decision whether to terminate the isola te or not. 317 a program that interfaces between the engine and the surrounding computing envir onment. The embedder will often be a web browser, but need not be; it may be a C ++ program on the server for example. When an isolate fails with a compile-time error as described above, control returns to the embedder, along with an excepti on describing the problem. This is necessary so that the embedder can clean up resources etc. It is then the embedder's decision whether to terminate the isola te or not.
318 } 318 }
319 319
320 \LMHash{} 320 \LMHash{}
321 {\em Static warnings} are those errors reported by the static checker. They hav e no effect on execution. Many, but not all, static warnings relate to types, in which case they are known as {\em static type warnings.} Static warnings must b e provided by Dart compilers used during development such as those incorporated in IDEs or otherwise intended to be used by developers for developing code. Comp ilers that are part of runtime execution environments such as virtual machines s hould not issue static warnings. 321 {\em Static warnings} are those errors reported by the static checker. They hav e no effect on execution. Many, but not all, static warnings relate to types, in which case they are known as {\em static type warnings.} Static warnings must b e provided by Dart compilers used during development such as those incorporated in IDEs or otherwise intended to be used by developers for developing code. Comp ilers that are part of runtime execution environments such as virtual machines s hould not issue static warnings.
322 322
323 \LMHash{} 323 \LMHash{}
324 {\em Dynamic type errors} are type errors reported in checked mode. 324 {\em Dynamic type errors} are type errors reported in checked mode.
325 325
326 \LMHash{} 326 \LMHash{}
327 {\em Run-time errors} are exceptions raised during execution. Whenever we say th at an exception $ex$ is {\em raised} or {\em thrown}, we mean that a throw expre ssion (\ref{throw}) of the form: \code{\THROW{} $ex$;} was implicitly evaluated or that a rethrow statement (\ref{rethrow}) of the form \code{\RETHROW} was exe cuted. When we say that {\em a} $C$ {\em is thrown}, where $C$ is a class, we me an that an instance of class $C$ is thrown. When we say that a stream raises an exception, we mean that an exception occurred while computing the value(s) of th e stream. 327 {\em Run-time errors} are exceptions thrown during execution. Whenever we say th at an exception $ex$ is {\em thrown}, it acts like an expression had {\em thrown } (\ref{completion}) with $ex$ as exception object and with a stack trace corres ponding to the current system state. When we say that {\em a} $C$ {\em is thrown }, where $C$ is a class, we mean that an instance of class $C$ is thrown.
328 328
329 \LMHash{} 329 \LMHash{}
330 If an uncaught exception is thrown by a running isolate $A$, $A$ is immediately suspended. 330 If an uncaught exception is thrown by a running isolate $A$, $A$ is immediately suspended.
331 331
332
333 \section{Variables} 332 \section{Variables}
334 \LMLabel{variables} 333 \LMLabel{variables}
335 334
336 \LMHash{} 335 \LMHash{}
337 Variables are storage locations in memory. 336 Variables are storage locations in memory.
338 337
339 \begin{grammar} 338 \begin{grammar}
340 {\bf variableDeclaration:} 339 {\bf variableDeclaration:}
341 declaredIdentifier (`,' identifier)* 340 declaredIdentifier (`,' identifier)*
342 . 341 .
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 795
797 \rationale{ 796 \rationale{
798 External functions allow us to introduce type information for code that is not statically known to the Dart compiler. 797 External functions allow us to introduce type information for code that is not statically known to the Dart compiler.
799 } 798 }
800 799
801 \commentary{ 800 \commentary{
802 Examples of external functions might be foreign functions (defined in C, or Java script etc.), primitives of the implementation (as defined by the Dart runtime), or code that was dynamically generated but whose interface is statically known. However, an abstract method is different from an external function, as it has { \em no} body. 801 Examples of external functions might be foreign functions (defined in C, or Java script etc.), primitives of the implementation (as defined by the Dart runtime), or code that was dynamically generated but whose interface is statically known. However, an abstract method is different from an external function, as it has { \em no} body.
803 } 802 }
804 803
805 \LMHash{} 804 \LMHash{}
806 An external function is connected to its body by an implementation specific mech anism. Attempting to invoke an external function that has not been connected to its body will raise a \code{NoSuchMethodError} or some subclass thereof. 805 An external function is connected to its body by an implementation specific mech anism. Attempting to invoke an external function that has not been connected to its body will throw a \code{NoSuchMethodError} or some subclass thereof.
807 806
808 \LMHash{} 807 \LMHash{}
809 The actual syntax is given in sections \ref{classes} and \ref{librariesAndScript s} below. 808 The actual syntax is given in sections \ref{classes} and \ref{librariesAndScript s} below.
810 809
811 \section{Classes} 810 \section{Classes}
812 \LMLabel{classes} 811 \LMLabel{classes}
813 812
814 \LMHash{} 813 \LMHash{}
815 A {\em class} defines the form and behavior of a set of objects which are its {\ em instances}. Classes may be defined by class declarations as described below, or via mixin applications (\ref{mixinApplication}). 814 A {\em class} defines the form and behavior of a set of objects which are its {\ em instances}. Classes may be defined by class declarations as described below, or via mixin applications (\ref{mixinApplication}).
816 815
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 1206
1208 % The enclosing scope of a generative constructor is the instance scope of the c lass in which it is declared (but what about redirecting?) 1207 % The enclosing scope of a generative constructor is the instance scope of the c lass in which it is declared (but what about redirecting?)
1209 1208
1210 \LMHash{} 1209 \LMHash{}
1211 Iff no constructor is specified for a class $C$, it implicitly has a default con structor \code{C() : \SUPER{}() \{\}}, unless $C$ is class \code{Object}. 1210 Iff no constructor is specified for a class $C$, it implicitly has a default con structor \code{C() : \SUPER{}() \{\}}, unless $C$ is class \code{Object}.
1212 1211
1213 \subsubsection{Generative Constructors} 1212 \subsubsection{Generative Constructors}
1214 \LMLabel{generativeConstructors} 1213 \LMLabel{generativeConstructors}
1215 1214
1216 \LMHash{} 1215 \LMHash{}
1217 A {\em generative constructor} consists of a constructor name, a constructor par ameter list, and either a redirect clause or an initializer list and an optiona l body. 1216 A {\em generative constructor} is executed to initialize a freshly allocated obj ect.
1217 It consists of a constructor name, a constructor parameter list, and either a re direct clause or an initializer list and an optional body.
1218 1218
1219 \begin{grammar} 1219 \begin{grammar}
1220 {\bf constructorSignature:} 1220 {\bf constructorSignature:}
1221 identifier (`{\escapegrammar .}' identifier)? formalParameterList 1221 identifier (`{\escapegrammar .}' identifier)? formalParameterList
1222 . 1222 .
1223 \end{grammar} 1223 \end{grammar}
1224 1224
1225 \LMHash{} 1225 \LMHash{}
1226 A {\em constructor parameter list} is a parenthesized, comma-separated list of f ormal constructor parameters. A {\em formal constructor parameter} is either a f ormal parameter (\ref{formalParameters}) or an initializing formal. An {\em init ializing formal} has the form \code{\THIS{}.id}, where \code{id} is the name of an instance variable of the immediately enclosing class. It is a compile-time e rror if \code{id} is not an instance variable of the immediately enclosing class . It is a compile-time error if an initializing formal is used by a function oth er than a non-redirecting generative constructor. 1226 A {\em constructor parameter list} is a parenthesized, comma-separated list of f ormal constructor parameters. A {\em formal constructor parameter} is either a f ormal parameter (\ref{formalParameters}) or an initializing formal. An {\em init ializing formal} has the form \code{\THIS{}.id}, where \code{id} is the name of an instance variable of the immediately enclosing class. It is a compile-time e rror if \code{id} is not an instance variable of the immediately enclosing class . It is a compile-time error if an initializing formal is used by a function oth er than a non-redirecting generative constructor.
1227 1227
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 1279
1280 \LMHash{} 1280 \LMHash{}
1281 A generative constructor may be {\em redirecting}, in which case its only action is to invoke another generative constructor. A redirecting constructor has no body; instead, it has a redirect clause that specifies which constructor the inv ocation is redirected to, and with what arguments. 1281 A generative constructor may be {\em redirecting}, in which case its only action is to invoke another generative constructor. A redirecting constructor has no body; instead, it has a redirect clause that specifies which constructor the inv ocation is redirected to, and with what arguments.
1282 1282
1283 \begin{grammar} 1283 \begin{grammar}
1284 {\bf redirection:} 1284 {\bf redirection:}
1285 `{\escapegrammar :}' \THIS{} (`{\escapegrammar .}' identifier)? arguments 1285 `{\escapegrammar :}' \THIS{} (`{\escapegrammar .}' identifier)? arguments
1286 . 1286 .
1287 \end{grammar} 1287 \end{grammar}
1288 1288
1289 A redirecting generative constructor is executed to initialize an object $o$ as
1290 follows:
1291
1292 Let $C$ be the class containing the redirecting generative constructor.
1293 Let $k$ the target generative constructor specified by either \THIS{} (the const ructor named $C$) or $\THIS{}.id$ (the constructor named $C.id$).
1294 Evaluate \code{arguments} to an argument list.
1295 Then bind the argument list to the formal parameters of $k$ and
1296 execute $k$ on the fresh object $o$.
1297
1298
1289 % Need to specify exactly how executing a redirecting constructor works 1299 % Need to specify exactly how executing a redirecting constructor works
1290 1300
1291 1301
1292 %\Q{We now have generative constructors with no bodies as well.} 1302 %\Q{We now have generative constructors with no bodies as well.}
1293 1303
1294 \paragraph{Initializer Lists} 1304 \paragraph{Initializer Lists}
1295 \LMLabel{initializerLists} 1305 \LMLabel{initializerLists}
1296 1306
1297 \LMHash{} 1307 \LMHash{}
1298 An initializer list begins with a colon, and consists of a comma-separated list of individual {\em initializers}. There are two kinds of initializers. 1308 An initializer list begins with a colon, and consists of a comma-separated list of individual {\em initializers}. There are two kinds of initializers.
(...skipping 20 matching lines...) Expand all
1319 1329
1320 \end{grammar} 1330 \end{grammar}
1321 1331
1322 \LMHash{} 1332 \LMHash{}
1323 Let $k$ be a generative constructor. Then $k$ may include at most one superini tializer in its initializer list or a compile-time error occurs. If no superinit ializer is provided, an implicit superinitializer of the form \SUPER{}() is adde d at the end of $k$'s initializer list, unless the enclosing class is class \cod e{Object}. It is a compile-time error if more than one initializer corresponding to a given instance variable appears in $k$'s initializer list. It is a compile -time error if $k$'s initializer list contains an initializer for a variable tha t is initialized by means of an initializing formal of $k$. % It is a compile-ti me error if $k$'s initializer list contains an initializer for a final variable $f$ whose declaration includes an initialization expression. 1333 Let $k$ be a generative constructor. Then $k$ may include at most one superini tializer in its initializer list or a compile-time error occurs. If no superinit ializer is provided, an implicit superinitializer of the form \SUPER{}() is adde d at the end of $k$'s initializer list, unless the enclosing class is class \cod e{Object}. It is a compile-time error if more than one initializer corresponding to a given instance variable appears in $k$'s initializer list. It is a compile -time error if $k$'s initializer list contains an initializer for a variable tha t is initialized by means of an initializing formal of $k$. % It is a compile-ti me error if $k$'s initializer list contains an initializer for a final variable $f$ whose declaration includes an initialization expression.
1324 1334
1325 \LMHash{} 1335 \LMHash{}
1326 Each final instance variable $f$ declared in the immediately enclosing class mus t have an initializer in $k$'s initializer list unless it has already been initi alized by one of the following means: 1336 Each final instance variable $f$ declared in the immediately enclosing class mus t have an initializer in $k$'s initializer list unless it has already been initi alized by one of the following means:
1327 \begin{itemize} 1337 \begin{itemize}
1328 \item Initialization at the declaration of $f$. 1338 \item Initialization at the declaration of $f$.
1329 \item Initialization by means of an initializing formal of $k$. 1339 \item Initialization by means of an initializing formal of $k$.
1330 \end{itemize} 1340 \end{itemize}
1331 1341
1332 or a static warning occurs. It is a compile-time error if $k$'s initializer list contains an initializer for a variable that is not an instance variable declare d in the immediately surrounding class. 1342 or a static warning occurs. It is a compile-time error if $k$'s initializer list contains an initializer for a variable that is not an instance variable declare d in the immediately surrounding class.
1333 1343
1334 1344
1335 \commentary{The initializer list may of course contain an initializer for any instance variable declared by the immediately surrounding class, even if it is n ot final. 1345 \commentary{The initializer list may of course contain an initializer for any instance variable declared by the immediately surrounding class, even if it is n ot final.
1336 } 1346 }
1337 1347
1338 \LMHash{} 1348 \LMHash{}
1339 It is a compile-time error if a generative constructor of class \code{Object} includes a superinitializer. 1349 It is a compile-time error if a generative constructor of class \code{Object} includes a superinitializer.
1340 1350
1341 \LMHash{} 1351 \LMHash{}
1342 Execution of a generative constructor $k$ is always done with respect to a set o f bindings for its formal parameters and with \THIS{} bound to a fresh instance $i$ and the type parameters of the immediately enclosing class bound to a set o f actual type arguments $V_1, \ldots , V_m$. 1352 Execution of a generative constructor $k$ to initialize a fresh instance $i$
1353 is always done with respect to a set of bindings for its formal parameters
1354 and the type parameters of the immediately enclosing class bound to a set of act ual type arguments $V_1, \ldots , V_m$.
1343 1355
1344 \commentary{These bindings are usually determined by the instance creation expre ssion that invoked the constructor (directly or indirectly). However, they may a lso be determined by a reflective call,. 1356 \commentary{These bindings are usually determined by the instance creation expre ssion that invoked the constructor (directly or indirectly). However, they may a lso be determined by a reflective call,.
1345 } 1357 }
1346 1358
1359
1347 \LMHash{} 1360 \LMHash{}
1348 If $k$ is redirecting then its redirect clause has the form 1361 If $k$ is redirecting then its redirect clause has the form
1349 1362
1350 \THIS{}$.g(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ 1363 \THIS{}$.g(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$
1351 1364
1352 where $g$ identifies another generative constructor of the immediately surround ing class. Then execution of $k$ proceeds by evaluating the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$, and then executing $g$ with respect to the bindings resulting from the evaluation of $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ and with \THIS{} bound to $i$ and the type parameters of the immediately enclosing class bound to $V_1, \ ldots , V_m$. 1365 where $g$ identifies another generative constructor of the immediately surround ing class. Then execution of $k$ to initialize $i$ proceeds by evaluating the ar gument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$, and then executing $g$ to initialize $i$ with respect to the bindings resulting from the evaluation of $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ and with \THIS{} bound to $i$ and the type parameters of the immediat ely enclosing class bound to $V_1, \ldots , V_m$.
1353 1366
1354 \LMHash{} 1367 \LMHash{}
1355 Otherwise, execution proceeds as follows: 1368 Otherwise, execution proceeds as follows:
1356 1369
1357 \LMHash{} 1370 \LMHash{}
1358 %First, a fresh instance (\ref{generativeConstructors}) $i$ of the immediately e nclosing class is allocated. Next, the instance variable declarations of the im mediately enclosing class are visited in the order they appear in the program te xt. For each such declaration $d$, if $d$ has the form \code{$finalConstVarOrTy pe$ $v$ = $e$; } then the instance variable $v$ of $i$ is bound to the value of $e$ (which is necessarily a compile-time constant). 1371 %First, a fresh instance (\ref{generativeConstructors}) $i$ of the immediately e nclosing class is allocated.
1372
1373 The instance variable declarations of the immediately enclosing class are visite d in the order they appear in the program text.
1374 For each such declaration $d$, if $d$ has the form \code{$finalConstVarOrType$ $ v$ = $e$; }
1375 then $e$ is evaluated to an object $o$
1376 and the instance variable $v$ of $i$ is bound to $o$.
1377
1359 %Next, a 1378 %Next, a
1360 Any initializing formals declared in $k$'s parameter list are executed in the or der they appear in the program text. 1379 Any initializing formals declared in $k$'s parameter list are executed in the or der they appear in the program text.
1361 % In fact, this order is unobservable; this could be done any time prior to runn ing the body, since 1380 % In fact, this order is unobservable; this could be done any time prior to runn ing the body, since
1362 % these only effect \THIS{}. 1381 % these only effect \THIS{}.
1363 Then, $k$'s initializers are executed in the order they appear in the program. 1382 Then, $k$'s initializers are executed to initialize $i$
1383 in the order they appear in the program.
1364 1384
1365 \rationale {We could observe the order by side effecting external routines call ed. So we need to specify the order.} 1385 \rationale {We could observe the order by side effecting external routines calle d. So we need to specify the order.}
1386
1387 Then if any instance variable of $i$ declared by the immediately enclosing class
1388 is not yet bound to a value,
1389 it is a dynamic error if such a variable is a \FINAL{} variable,
1390 otherwise all such variables are initialized with the \NULL{} value.
1391
1392 After this, unless the enclosing class is \code{Object}, the super constructor c all implicitly or explicitly specified in $k$'s initializers, is now executed to further initialize $i$, as specified below.
1393
1394 \commentary{
1395 The super constructor call can be written anywhere in the initializers of $k$, b ut the actual call always happens after all initializers have been processed.
1396 It is not equivalent to moving the super call to the end of the initializers
1397 because the argument expressions may have visible side effects
1398 which must happen in the order the expressions occur in the program text.
1399 }
1366 1400
1367 \LMHash{} 1401 \LMHash{}
1368 After all the initializers have completed, the body of $k$ is executed in a sc ope where \THIS{} is bound to $i$. Execution of the body begins with execution o f the body of the superconstructor with \THIS{} bound to $i$, the type paramete rs of the immediately enclosing class bound to a set of actual type arguments $V _1, \ldots , V_m$ and the formal parameters bindings determined by the argument list of the superinitializer of $k$. 1402 After all superclass constructors have completed, the body of $k$ is executed in a scope where \THIS{} is bound to $i$.
1369 1403
1370 \rationale{ 1404 \rationale{
1371 This process ensures that no uninitialized final field is ever seen by code. Not e that \THIS{} is not in scope on the right hand side of an initializer (see \re f{this}) so no instance method can execute during initialization: an instance me thod cannot be directly invoked, nor can \THIS{} be passed into any other code being invoked in the initializer. 1405 This process ensures that no uninitialized final field is ever seen by code. Not e that \THIS{} is not in scope on the right hand side of an initializer (see \re f{this}) so no instance method can execute during initialization: an instance me thod cannot be directly invoked, nor can \THIS{} be passed into any other code being invoked in the initializer.
1372 } 1406 }
1373 1407
1374 \LMHash{} 1408 \LMHash{}
1375 Execution of an initializer of the form \code{\THIS{}.$v$ = $e$} proceeds as fol lows: 1409 Execution of an initializer of the form \code{\THIS{}.$v$ = $e$} to initialize a n object $i$ proceeds as follows:
1376 1410
1377 \LMHash{} 1411 \LMHash{}
1378 First, the expression $e$ is evaluated to an object $o$. Then, the instance vari able $v$ of the object denoted by \THIS{} is bound to $o$, unless $v$ is a final variable that has already been initialized, in which case a runtime error occur s. In checked mode, it is a dynamic type error if $o$ is not \NULL{} and the int erface of the class of $o$ is not a subtype of the actual type of the field $v$. 1412 First, the expression $e$ is evaluated to an object $o$. Then, the instance vari able $v$ of $i$ is bound to $o$, unless $v$ is a final variable that has already been initialized, in which case a runtime error occurs. In checked mode, it is a dynamic type error if $o$ is not \NULL{} and the interface of the class of $o$ is not a subtype of the actual type of the field $v$.
1379 1413
1380 \LMHash{} 1414 \LMHash{}
1381 An initializer of the form \code{$v$ = $e$} is equivalent to an initializer of t he form \code{\THIS{}.$v$ = $e$}. 1415 An initializer of the form \code{$v$ = $e$} is equivalent to an initializer of t he form \code{\THIS{}.$v$ = $e$}.
1382 1416
1383 \LMHash{} 1417 \LMHash{}
1384 Execution of a superinitializer of the form 1418 Execution of a superinitializer of the form
1385 1419
1386 \SUPER{}$(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$ 1420 \SUPER{}$(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$
1387 1421
1388 (respectively \SUPER{}$.id(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$ 1422 (respectively \SUPER{}$.id(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$
1389 1423
1390 proceeds as follows: 1424 proceeds as follows:
1391 1425
1392 \LMHash{} 1426 \LMHash{}
1393 First, the argument list $(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$ is evaluated. 1427 First, the argument list $(a_1, \ldots, a_n, x_{n+1}: a_{n+1}, \ldots, x_{n+k}: a_{n+k})$ is evaluated.
1428 This evaluated argument list is remembered until after the entire initializer li st has been evaluated, at which point the constructor is executed as follows:
1394 1429
1395 \LMHash{} 1430 \LMHash{}
1396 Let $C$ be the class in which the superinitializer appears and let $S$ be the su perclass of $C$. If $S$ is generic (\ref{generics}), let $U_1, , \ldots, U_m$ b e the actual type arguments passed to $S$ in the superclass clause of $C$. 1431 Let $C$ be the class in which the superinitializer appears and let $S$ be the su perclass of $C$. If $S$ is generic (\ref{generics}), let $U_1, , \ldots, U_m$ b e the actual type arguments passed to $S$ in the superclass clause of $C$.
1397 1432
1398 \LMHash{} 1433 \LMHash{}
1399 Then, the initializer list of the constructor $S$ (respectively $S.id$) is execu ted with respect to the bindings that resulted from the evaluation of the argume nt list, with \THIS{} bound to the current binding of \THIS{}, and the type pa rameters (if any) of class $S$ bound to the current bindings of $U_1, , \ldots, U_m$. 1434 The generative constructor $S$ (respectively $S.id$) of $S$ is executed to initi alize $i$ with respect to the bindings that resulted from the evaluation of the argument list, and the type parameters (if any) of class $S$ bound to the curren t bindings of $U_1, , \ldots, U_m$.
1400 1435
1401 \LMHash{} 1436 \LMHash{}
1402 It is a compile-time error if class $S$ does not declare a generative constructo r named $S$ (respectively $S.id$). 1437 It is a compile-time error if class $S$ does not declare a generative constructo r named $S$ (respectively $S.id$).
1403 1438
1404 \subsubsection{Factories} 1439 \subsubsection{Factories}
1405 \LMLabel{factories} 1440 \LMLabel{factories}
1406 1441
1407 \LMHash{} 1442 \LMHash{}
1408 A {\em factory} is a constructor prefaced by the built-in identifier (\ref{iden tifierReference}) \FACTORY{}. 1443 A {\em factory} is a constructor prefaced by the built-in identifier (\ref{iden tifierReference}) \FACTORY{}.
1409 1444
(...skipping 12 matching lines...) Expand all
1422 \LMHash{} 1457 \LMHash{}
1423 It is a compile-time error if $M$ is not the name of the immediately enclosing c lass. 1458 It is a compile-time error if $M$ is not the name of the immediately enclosing c lass.
1424 1459
1425 \LMHash{} 1460 \LMHash{}
1426 In checked mode, it is a dynamic type error if a factory returns a non-null obje ct whose type is not a subtype of its actual (\ref{actualTypeOfADeclaration}) re turn type. 1461 In checked mode, it is a dynamic type error if a factory returns a non-null obje ct whose type is not a subtype of its actual (\ref{actualTypeOfADeclaration}) re turn type.
1427 1462
1428 \rationale{It seems useless to allow a factory to return null. But it is more un iform to allow it, as the rules currently do.} 1463 \rationale{It seems useless to allow a factory to return null. But it is more un iform to allow it, as the rules currently do.}
1429 1464
1430 \rationale{Factories address classic weaknesses associated with constructors in other languages. 1465 \rationale{Factories address classic weaknesses associated with constructors in other languages.
1431 Factories can produce instances that are not freshly allocated: they can come fr om a cache. Likewise, factories can return instances of different classes. 1466 Factories can produce instances that are not freshly allocated: they can come fr om a cache. Likewise, factories can return instances of different classes.
1432
1433 } 1467 }
1434 1468
1435 \paragraph{Redirecting Factory Constructors} 1469 \paragraph{Redirecting Factory Constructors}
1436 \LMLabel{redirectingFactoryConstructors} 1470 \LMLabel{redirectingFactoryConstructors}
1437 1471
1438 \LMHash{} 1472 \LMHash{}
1439 A {\em redirecting factory constructor} specifies a call to a constructor of ano ther class that is to be used whenever the redirecting constructor is called. 1473 A {\em redirecting factory constructor} specifies a call to a constructor of ano ther class that is to be used whenever the redirecting constructor is called.
1440 1474
1441 \begin{grammar} 1475 \begin{grammar}
1442 {\bf redirectingFactoryConstructorSignature:} 1476 {\bf redirectingFactoryConstructorSignature:}
(...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after
2330 Metadata can appear before a library, part header, class, typedef, type paramete r, constructor, factory, function, field, parameter, or variable declaration and before an import, export or part directive. 2364 Metadata can appear before a library, part header, class, typedef, type paramete r, constructor, factory, function, field, parameter, or variable declaration and before an import, export or part directive.
2331 2365
2332 \LMHash{} 2366 \LMHash{}
2333 The constant expression given in an annotation is type checked and evaluated in the scope surrounding the declaration being annotated. 2367 The constant expression given in an annotation is type checked and evaluated in the scope surrounding the declaration being annotated.
2334 2368
2335 2369
2336 \section{Expressions} 2370 \section{Expressions}
2337 \LMLabel{expressions} 2371 \LMLabel{expressions}
2338 2372
2339 \LMHash{} 2373 \LMHash{}
2340 An {\em expression} is a fragment of Dart code that can be evaluated at run time to yield a {\em value}, which is always an object. Every expression has an asso ciated static type (\ref{staticTypes}). Every value has an associated dynamic ty pe (\ref{dynamicTypeSystem}). 2374 \LMLabel{evaluation}
2375 An {\em expression} is a fragment of Dart code that can be evaluated at run time .
2376 Evaluating an expression either {\em produces a value} (an object), or it {\em t hrows} an exception object and an associated stack trace.
2377 In the former case, we also say that the expression {\em evaluates to a value}.
2378
2379 Every expression has an associated static type (\ref{staticTypes}).
2380 Every value has an associated dynamic type (\ref{dynamicTypeSystem}).
2381
2382 If evaluation of an expression is defined in terms of evaluation of another expr ession,
2383 and the evaluation of the other expression throws an exception and a stack trace ,
2384 if nothing else is stated, the evaluation of the first expression stops
2385 at that point and throws the same exception object and stack trace.
2341 2386
2342 2387
2343 \begin{grammar} 2388 \begin{grammar}
2344 2389
2345 {\bf expression:}assignableExpression assignmentOperator expression; 2390 {\bf expression:}assignableExpression assignmentOperator expression;
2346 conditionalExpression cascadeSection*; 2391 conditionalExpression cascadeSection*;
2347 throwExpression 2392 throwExpression
2348 . 2393 .
2349 2394
2350 2395
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
2451 \item An expression of the form \code{$e_1$?$e_2$:$e3$} where $e_1$, $e_2$ and $ e_3$ are constant expressions and $e_1$ evaluates to a boolean value. 2496 \item An expression of the form \code{$e_1$?$e_2$:$e3$} where $e_1$, $e_2$ and $ e_3$ are constant expressions and $e_1$ evaluates to a boolean value.
2452 \item An expression of the form \code{$e_1 ?? e_2$} where $e_1$ and $e_2$ are co nstant expressions. 2497 \item An expression of the form \code{$e_1 ?? e_2$} where $e_1$ and $e_2$ are co nstant expressions.
2453 \item An expression of the form \code{$e$.length} where $e$ is a constant expres sion that evaluates to a string value. 2498 \item An expression of the form \code{$e$.length} where $e$ is a constant expres sion that evaluates to a string value.
2454 \end{itemize} 2499 \end{itemize}
2455 2500
2456 % null in all the expressions 2501 % null in all the expressions
2457 2502
2458 % designed so constants do not depend on check diode being on or not. 2503 % designed so constants do not depend on check diode being on or not.
2459 2504
2460 \LMHash{} 2505 \LMHash{}
2461 It is a compile-time error if an expression is required to be a constant express ion but its evaluation would raise an exception. 2506 It is a compile-time error if an expression is required to be a constant express ion but its evaluation would throw an exception.
2462 2507
2463 % so, checked mode? analyzers? editor/development compilers? 2508 % so, checked mode? analyzers? editor/development compilers?
2464 \commentary{ 2509 \commentary{
2465 Note that there is no requirement that every constant expression evaluate correc tly. Only when a constant expression is required (e.g., to initialize a constant variable, or as a default value of a formal parameter, or as metadata) do we in sist that a constant expression actually be evaluated successfully at compile ti me. 2510 Note that there is no requirement that every constant expression evaluate correc tly. Only when a constant expression is required (e.g., to initialize a constant variable, or as a default value of a formal parameter, or as metadata) do we in sist that a constant expression actually be evaluated successfully at compile ti me.
2466 2511
2467 The above is not dependent on program control-flow. The mere presence of a requi red compile time constant whose evaluation would fail within a program is an err or. This also holds recursively: since compound constants are composed out of c onstants, if any subpart of a constant would raise an exception when evaluated, that is an error. 2512 The above is not dependent on program control-flow. The mere presence of a requi red compile time constant whose evaluation would fail within a program is an err or. This also holds recursively: since compound constants are composed out of c onstants, if any subpart of a constant would throw an exception when evaluated, that is an error.
2468 2513
2469 On the other hand, since implementations are free to compile code late, some com pile-time errors may manifest quite late. 2514 On the other hand, since implementations are free to compile code late, some com pile-time errors may manifest quite late.
2470 } 2515 }
2471 2516
2472 \begin{dartCode} 2517 \begin{dartCode}
2473 \CONST{} x = 1 ~/ 0; 2518 \CONST{} x = 1 ~/ 0;
2474 \FINAL{} y = 1 ~/ 0; 2519 \FINAL{} y = 1 ~/ 0;
2475 2520
2476 \CLASS{} K \{ 2521 \CLASS{} K \{
2477 m1() \{ 2522 m1() \{
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
2534 listLiteral 2579 listLiteral
2535 . 2580 .
2536 \end{grammar} 2581 \end{grammar}
2537 2582
2538 2583
2539 2584
2540 \subsection{Null} 2585 \subsection{Null}
2541 \LMLabel{null} 2586 \LMLabel{null}
2542 2587
2543 \LMHash{} 2588 \LMHash{}
2544 The reserved word \NULL{} denotes the {\em null object}. 2589 The reserved word \NULL{} evaluates to the {\em null object}.
2545 %\Q{Any methods, such as \code{isNull}?} 2590 %\Q{Any methods, such as \code{isNull}?}
2546 2591
2547 \begin{grammar} 2592 \begin{grammar}
2548 {\bf nullLiteral:} 2593 {\bf nullLiteral:}
2549 \NULL{} 2594 \NULL{}
2550 . 2595 .
2551 \end{grammar} 2596 \end{grammar}
2552 2597
2553 \LMHash{} 2598 \LMHash{}
2554 The null object is the sole instance of the built-in class \code{Null}. Attempti ng to instantiate \code{Null} causes a run-time error. It is a compile-time erro r for a class to attempt to extend, mix in or implement \code{Null}. 2599 The null object is the sole instance of the built-in class \code{Null}. Attempti ng to instantiate \code{Null} causes a run-time error. It is a compile-time erro r for a class to attempt to extend, mix in or implement \code{Null}.
2555 Invoking a method on \NULL{} yields a \code{NoSuchMethodError} unless the metho d is explicitly implemented by class \code{Null}. 2600 The \code{Null} class declares no methods except those also declared by \code{Ob ject}.
2556 2601
2557 \LMHash{} 2602 \LMHash{}
2558 The static type of \NULL{} is $\bot$. 2603 The static type of \NULL{} is $\bot$.
2559 2604
2560 \rationale{The decision to use $\bot$ instead of \code{Null} allows \NULL{} to b e be assigned everywhere without complaint by the static checker. 2605 \rationale{The decision to use $\bot$ instead of \code{Null} allows \NULL{} to b e be assigned everywhere without complaint by the static checker.
2561 } 2606 }
2562 2607
2563 2608
2564 \subsection{Numbers} 2609 \subsection{Numbers}
2565 \LMLabel{numbers} 2610 \LMLabel{numbers}
(...skipping 18 matching lines...) Expand all
2584 `0X' HEX\_DIGIT+ 2629 `0X' HEX\_DIGIT+
2585 . 2630 .
2586 2631
2587 {\bf HEX\_DIGIT:}`a'{\escapegrammar ..}'f'; 2632 {\bf HEX\_DIGIT:}`a'{\escapegrammar ..}'f';
2588 `A'{\escapegrammar ..}'F'; 2633 `A'{\escapegrammar ..}'F';
2589 DIGIT 2634 DIGIT
2590 . 2635 .
2591 \end{grammar} 2636 \end{grammar}
2592 2637
2593 \LMHash{} 2638 \LMHash{}
2594 If a numeric literal begins with the prefix `0x' or `0X', it denotes the hexadec imal integer represented by the part of the literal following `0x' (respectively `0X'). Otherwise, if the numeric literal contains only decimal digits, it denot es a decimal integer. Otherwise, the numeric literal contains either a decimal point or an exponent part and it denotes a 64 bit double precision floating poin t number as specified by the IEEE 754 standard. 2639 If a numeric literal begins with the prefix `0x' or `0X',
2640 it denotes the hexadecimal integer numeral
2641 represented by the part of the literal following `0x' (respectively `0X').
2642 Otherwise, if the numeric literal contains only decimal digits,
2643 it denotes a decimal integer numeral.
2644 In either case the literal evaluates to an instance of the class \code{int}
2645 with the integer value represented by that numeral.
2646 Otherwise, the numeric literal contains either a decimal point or an exponent pa rt
2647 and it evaluates to a an instance of the `double` class
2648 representing a 64 bit double precision floating point number
2649 as specified by the IEEE 754 standard.
2595 2650
2596 \LMHash{} 2651 \LMHash{}
2597 In principle, the range of integers supported by a Dart implementations is unlim ited. In practice, it is limited by available memory. Implementations may also b e limited by other considerations. 2652 In principle, the range of integers supported by a Dart implementations is unlim ited. In practice, it is limited by available memory. Implementations may also b e limited by other considerations.
2598 2653
2599 \commentary{ 2654 \commentary{
2600 For example, implementations may choose to limit the range to facilitate efficie nt compilation to Javascript. These limitations should be relaxed as soon as tec hnologically feasible. 2655 For example, implementations may choose to limit the range to facilitate efficie nt compilation to Javascript. These limitations should be relaxed as soon as tec hnologically feasible.
2601 } 2656 }
2602 2657
2603 \LMHash{} 2658 \LMHash{}
2604 It is a compile-time error for a class to attempt to extend, mix in or implement \code{int}. It is a compile-time error for a class to attempt to extend, mix in or implement \code{double}. It is a compile-time error for any type other than the types \code{int} and \code{double} to attempt to extend, mix in or implement \code{num}. 2659 It is a compile-time error for a class to attempt to extend, mix in or implement \code{int}. It is a compile-time error for a class to attempt to extend, mix in or implement \code{double}. It is a compile-time error for any type other than the types \code{int} and \code{double} to attempt to extend, mix in or implement \code{num}.
2605 2660
2606 \LMHash{} 2661 \LMHash{}
2607 An {\em integer literal} is either a hexadecimal integer literal or a decimal i nteger literal. Invoking the getter \code{runtimeType} on an integer literal ret urns the \code{Type} object that is the value of the expression \code{int}. The static type of an integer literal is \code{int}. 2662 An {\em integer literal} is either a hexadecimal integer literal or a decimal i nteger literal.
2663 The static type of an integer literal is \code{int}.
2608 2664
2609 \LMHash{} 2665 \LMHash{}
2610 A {\em literal double} is a numeric literal that is not an integer literal. Invo king the getter \code{runtimeType} on a literal double returns the \code{Type} o bject that is the value of the expression \code{double}. 2666 A {\em literal double} is a numeric literal that is not an integer literal. The static type of a literal double is \code{double}.
2611 The static type of a literal double is \code{double}.
2612 2667
2613 \subsection{Booleans} 2668 \subsection{Booleans}
2614 \LMLabel{booleans} 2669 \LMLabel{booleans}
2615 2670
2616 \LMHash{} 2671 \LMHash{}
2617 The reserved words \TRUE{} and \FALSE{} denote objects that represent the boolea n values true and false respectively. They are the {\em boolean literals}. 2672 The reserved words \TRUE{} and \FALSE{} evaluate to objects {\em true} and {\em false} that represent the boolean values true and false respectively.
2673 They are the {\em boolean literals}.
2618 2674
2619 \begin{grammar} 2675 \begin{grammar}
2620 {\bf booleanLiteral:}\TRUE{}; 2676 {\bf booleanLiteral:}\TRUE{};
2621 \FALSE{} 2677 \FALSE{}
2622 . 2678 .
2623 \end{grammar} 2679 \end{grammar}
2624 2680
2625 \LMHash{} 2681 \LMHash{}
2626 Both \TRUE{} and \FALSE{} implement the built-in class \code{bool}. It is a co mpile-time error for a class to attempt to extend, mix in or implement\code{ boo l}. 2682 Both {\em true} and {\em false} are instances of the built-in class \code{bool},
2627 2683 and there are no other objects that implement \code{bool}.
2628 \commentary{ 2684 It is a compile-time error for a class to attempt to extend, mix in or implement \code{ bool}.
2629 It follows that the two boolean literals are the only two instances of \code{boo l}.
2630 }
2631 2685
2632 \LMHash{} 2686 \LMHash{}
2633 Invoking the getter \code{runtimeType} on a boolean literal returns the \code{Ty pe} object that is the value of the expression \code{bool}. The static type of a boolean literal is \code{bool}. 2687 Invoking the getter \code{runtimeType} on a boolean value returns the \code{Type } object that is the value of the expression \code{bool}. The static type of a b oolean literal is \code{bool}.
2634 2688
2635 \subsubsection{Boolean Conversion} 2689 \subsubsection{Boolean Conversion}
2636 \LMLabel{booleanConversion} 2690 \LMLabel{booleanConversion}
2637 2691
2638 \LMHash{} 2692 \LMHash{}
2639 {\em Boolean conversion} maps any object $o$ into a boolean. Boolean conversion is defined by the function application 2693 {\em Boolean conversion} maps any object $o$ into a boolean. Boolean conversion is defined by the function application
2640 2694
2641 \begin{dartCode} 2695 \begin{dartCode}
2642 (bool v)\{ 2696 (bool v)\{
2643 \ASSERT{}(v != \NULL{}); 2697 \ASSERT{}(v != \NULL{});
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
2824 stringInterpolation 2878 stringInterpolation
2825 . 2879 .
2826 2880
2827 {\bf NEWLINE:}$\backslash$ n; 2881 {\bf NEWLINE:}$\backslash$ n;
2828 $\backslash$ r 2882 $\backslash$ r
2829 . 2883 .
2830 2884
2831 \end{grammar} 2885 \end{grammar}
2832 2886
2833 \LMHash{} 2887 \LMHash{}
2834 All string literals implement the built-in class \code{String}. It is a compile- time error for a class to attempt to extend, mix in or implement \code{String}. Invoking the getter \code{runtimeType} on a string literal returns the \code{Typ e} object that is the value of the expression \code{String}. The static type of a string literal is \code{String}. 2888 All string literals evaluates to instances of the built-in class \code{String}. It is a compile-time error for a class to attempt to extend, mix in or implement \code{String}.
2889 The static type of a string literal is \code{String}.
2835 2890
2836 \subsubsection{String Interpolation} 2891 \subsubsection{String Interpolation}
2837 \LMLabel{stringInterpolation} 2892 \LMLabel{stringInterpolation}
2838 2893
2839 \LMHash{} 2894 \LMHash{}
2840 It is possible to embed expressions within non-raw string literals, such that th ese expressions are evaluated, and the resulting values are converted into strin gs and concatenated with the enclosing string. This process is known as {\em str ing interpolation}. 2895 It is possible to embed expressions within non-raw string literals, such that th ese expressions are evaluated, and the resulting values are converted into strin gs and concatenated with the enclosing string. This process is known as {\em str ing interpolation}.
2841 2896
2842 \begin{grammar} 2897 \begin{grammar}
2843 {\bf stringInterpolation:}`\$' IDENTIFIER\_NO\_DOLLAR; 2898 {\bf stringInterpolation:}`\$' IDENTIFIER\_NO\_DOLLAR;
2844 `\$' `\{' expression `\}' % could be top level expression, no? 2899 `\$' `\{' expression `\}' % could be top level expression, no?
2845 . 2900 .
2846 \end{grammar} 2901 \end{grammar}
2847 2902
2848 \commentary{The reader will note that the expression inside the interpolation co uld itself include strings, which could again be interpolated recursively. 2903 \commentary{The reader will note that the expression inside the interpolation co uld itself include strings, which could again be interpolated recursively.
2849 } 2904 }
2850 2905
2851 \LMHash{} 2906 \LMHash{}
2852 An unescaped \$ character in a string signifies the beginning of an interpolated expression. The \$ sign may be followed by either: 2907 An unescaped \$ character in a string signifies the beginning of an interpolated expression. The \$ sign may be followed by either:
2853 \begin{itemize} 2908 \begin{itemize}
2854 \item A single identifier $id$ that must not contain the \$ character. 2909 \item A single identifier $id$ that must not contain the \$ character.
2855 \item An expression $e$ delimited by curly braces. 2910 \item An expression $e$ delimited by curly braces.
2856 \end{itemize} 2911 \end{itemize}
2857 2912
2858 \LMHash{} 2913 \LMHash{}
2859 The form \code{\$id} is equivalent to the form \code{\$\{id\}}. An interpolated string \code{`$s_1$\$\{$e$\}$s_2$'} is equivalent to the concatenation of the strings \code{`$s_1$'}, \code{$e$.toString()} and \code{$`s_2$'}. Likewise an interpolated string \code{``$s_1$\$\{e\}$s_2$''} is equivalent to the concatena tion of the strings \code{``$s_1$''}, \code{$e$.toString()} and \code{``$s_2$'' }. 2914 The form \code{\$id} is equivalent to the form \code{\$\{id\}}.
2915 An interpolated string, $s$, with content `\code{$s_0$\$\{$e_1$\}$s_1\ldots{}s_{ n-1}\$\{e_n\}s_{n}$}' (where any of $s_0 \ldots{} s_n$ can be empty)
2916 is evaluated by evaluating each expression $e_i$ ($1 \le i \le n$) in to a strin g $r_i$ in the order they occur in the source text, as follows:
2917 \begin{itemize}
2918 \item{} Evaluate $e_i$ to an object $o_i$.
2919 \item{} Invoke the \code{toString} method on $o_i$ with no arguments, and let
2920 $r_i$ be the returned value.
2921 \item{} If $r_i$ is not an instance of the built-in type \code{String}, throw an \code{Error}.
2922 \end{itemize}
2923 Finally, the result of the evaluation of $s$ is the concatenation of the strings $s_0$, $r_1$, \ldots{}, $r_n$, and $s_n$.
2860 2924
2861 \subsection{Symbols} 2925 \subsection{Symbols}
2862 \LMLabel{symbols} 2926 \LMLabel{symbols}
2863 2927
2864 \LMHash{} 2928 \LMHash{}
2865 A {\em symbol literal} denotes the name of a declaration in a Dart program. 2929 A {\em symbol literal} denotes the name of a declaration in a Dart program.
2866 2930
2867 \begin{grammar} 2931 \begin{grammar}
2868 {\bf symbolLiteral:} 2932 {\bf symbolLiteral:}
2869 `\#' (operator $|$ (identifier (`{\escapegrammar .}' identifier)*)) . 2933 `\#' (operator $|$ (identifier (`{\escapegrammar .}' identifier)*)) .
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2913 2977
2914 \LMHash{} 2978 \LMHash{}
2915 Let $list_1 =$ \CONST{} $<V>[e_{11} \ldots e_{1n}]$ and $list_2 =$ \CONST{} $<U >[e_{21} \ldots e_{2n}]$ be two constant list literals and let the elements of $list_1$ and $list_2$ evaluate to $o_{11} \ldots o_{1n}$ and $o_{21} \ldots o_ {2n}$ respectively. Iff \code{identical($o_{1i}$, $o_{2i}$)} for $i \in 1.. n$ a nd $V = U$ then \code{identical($list_1$, $list_2$)}. 2979 Let $list_1 =$ \CONST{} $<V>[e_{11} \ldots e_{1n}]$ and $list_2 =$ \CONST{} $<U >[e_{21} \ldots e_{2n}]$ be two constant list literals and let the elements of $list_1$ and $list_2$ evaluate to $o_{11} \ldots o_{1n}$ and $o_{21} \ldots o_ {2n}$ respectively. Iff \code{identical($o_{1i}$, $o_{2i}$)} for $i \in 1.. n$ a nd $V = U$ then \code{identical($list_1$, $list_2$)}.
2916 2980
2917 \commentary{In other words, constant list literals are canonicalized.} 2981 \commentary{In other words, constant list literals are canonicalized.}
2918 2982
2919 \LMHash{} 2983 \LMHash{}
2920 A run-time list literal $<E>[e_1 \ldots e_n]$ is evaluated as follows: 2984 A run-time list literal $<E>[e_1 \ldots e_n]$ is evaluated as follows:
2921 \begin{itemize} 2985 \begin{itemize}
2922 \item 2986 \item
2923 First, the expressions $e_1 \ldots e_n$ are evaluated in order they appear in th e program, yielding objects $o_1 \ldots o_n$. 2987 First, the expressions $e_1 \ldots e_n$ are evaluated in order they appear in th e program, producing objects $o_1 \ldots o_n$.
2924 \item A fresh instance (\ref{generativeConstructors}) $a$, of size $n$, whose class implements the built-in class $List<E>$ is allocated. 2988 \item A fresh instance (\ref{generativeConstructors}) $a$, of size $n$, whose class implements the built-in class $List<E>$ is allocated.
2925 \item 2989 \item
2926 The operator \code{[]=} is invoked on $a$ with first argument $i$ and second a rgument 2990 The operator \code{[]=} is invoked on $a$ with first argument $i$ and second a rgument
2927 %The $i$th element of $a$ is set to 2991 %The $i$th element of $a$ is set to
2928 $o_{i+1}, 0 \le i < n$. 2992 $o_{i+1}, 0 \le i < n$.
2929 \item 2993 \item
2930 The result of the evaluation is $a$. 2994 The result of the evaluation is $a$.
2931 \end{itemize} 2995 \end{itemize}
2932 2996
2933 2997
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2989 3053
2990 \LMHash{} 3054 \LMHash{}
2991 Let $map_1 =$ \CONST{}$ <K, V>\{k_{11}:e_{11} \ldots k_{1n} :e_{1n}\}$ and $map _2 =$ \CONST{}$ <J, U>\{k_{21}:e_{21} \ldots k_{2n} :e_{2n}\}$ be two constant map literals. Let the keys of $map_1$ and $map_2$ evaluate to $s_{11} \ldots s _{1n}$ and $s_{21} \ldots s_{2n}$ respectively, and let the elements of $map _1$ and $map_2$ evaluate to $o_{11} \ldots o_{1n}$ and $o_{21} \ldots o_{2n}$ respectively. Iff \code{identical($o_{1i}$, $o_{2i}$)} and \code{identical($s_{ 1i}$, $s_{2i}$)} for $i \in 1.. n$, and $K = J, V = U$ then \code{identical($map _1$, $map_2$)}. 3055 Let $map_1 =$ \CONST{}$ <K, V>\{k_{11}:e_{11} \ldots k_{1n} :e_{1n}\}$ and $map _2 =$ \CONST{}$ <J, U>\{k_{21}:e_{21} \ldots k_{2n} :e_{2n}\}$ be two constant map literals. Let the keys of $map_1$ and $map_2$ evaluate to $s_{11} \ldots s _{1n}$ and $s_{21} \ldots s_{2n}$ respectively, and let the elements of $map _1$ and $map_2$ evaluate to $o_{11} \ldots o_{1n}$ and $o_{21} \ldots o_{2n}$ respectively. Iff \code{identical($o_{1i}$, $o_{2i}$)} and \code{identical($s_{ 1i}$, $s_{2i}$)} for $i \in 1.. n$, and $K = J, V = U$ then \code{identical($map _1$, $map_2$)}.
2992 3056
2993 \commentary{In other words, constant map literals are canonicalized.} 3057 \commentary{In other words, constant map literals are canonicalized.}
2994 3058
2995 \LMHash{} 3059 \LMHash{}
2996 A runtime map literal $<K, V>\{k_1:e_1\ldots k_n :e_n\}$ is evaluated as follow s: 3060 A runtime map literal $<K, V>\{k_1:e_1\ldots k_n :e_n\}$ is evaluated as follow s:
2997 \begin{itemize} 3061 \begin{itemize}
2998 \item 3062 \item
2999 First, the expression $k_i$ is evaluated yielding object $u_i$, the $e_i$ is vau lted yielding object $o_i$, for $i \in 1..n$ in left to right order, yielding ob jects $u_1, o_1\ldots u_n, o_n$. 3063 For each $i \in 1..n$ in numeric order,
3000 \item A fresh instance (\ref{generativeConstructors}) $m$ whose class implement s the built-in class 3064 first the expression $k_i$ is evaluated producing object $u_i$,
3001 3065 and then $e_i$ is evaluated producing object $o_i$.
3002 $Map<K, V>$ is allocated. 3066 This produces all the objects $u_1, o_1\ldots u_n, o_n$.
3067 \item A fresh instance (\ref{generativeConstructors}) $m$ whose class implement s the built-in class $Map<K, V>$ is allocated.
3003 \item 3068 \item
3004 The operator \code{[]=} is invoked on $m$ with first argument $u_i$ and second argument $o_i, i \in 1.. n$. 3069 The operator \code{[]=} is invoked on $m$ with first argument $u_i$ and second argument $o_i$ for each $i \in 1.. n$.
3005 \item 3070 \item
3006 The result of the evaluation is $m$. 3071 The result of the evaluation is $m$.
3007 \end{itemize} 3072 \end{itemize}
3008 3073
3009 3074
3010 \LMHash{} 3075 \LMHash{}
3011 A runtime map literal $\{k_1:e_1\ldots k_n :e_n\}$ is evaluated as 3076 A runtime map literal $\{k_1:e_1\ldots k_n :e_n\}$ is evaluated as
3012 3077
3013 $<\DYNAMIC{}, \DYNAMIC{}>\{k_1:e_1\ldots k_n :e_n\}$. 3078 $<\DYNAMIC{}, \DYNAMIC{}>\{k_1:e_1\ldots k_n :e_n\}$.
3014 3079
3015 \LMHash{} 3080 \LMHash{}
3016 Iff all the keys in a map literal are compile-time constants, it is a static war ning if the values of any two keys in a map literal are equal. 3081 Iff all the keys in a map literal are compile-time constants, it is a static war ning if the values of any two keys in a map literal are equal.
3017 3082
3018 \LMHash{} 3083 \LMHash{}
3019 A map literal is ordered: iterating over the keys and/or values of the maps alwa ys happens in the 3084 A map literal is ordered: iterating over the keys and/or values of the maps alwa ys happens in the
3020 order the keys appeared in the source code. 3085 order the keys appeared in the source code.
3021 3086
3022 \commentary{ 3087 \commentary{
3023 Of course, if a key repeats, the order is defined by first occurrence, but the v alue is defined by the last. 3088 Of course, if a key repeats, the order is defined by first occurrence, but the v alue is defined by the last.
3024 } 3089 }
3025 3090
3026 \LMHash{} 3091 \LMHash{}
3027 The static type of a map literal of the form \CONST{}$ <K, V>\{k_1:e_1\ldots k_ n :e_n\}$ or the form $<K, V>\{k_1:e_1\ldots k_n :e_n\}$ is $Map<K, V>$. The sta tic type a map literal of the form \CONST{}$\{k_1:e_1\ldots k_n :e_n\}$ or the form $\{k_1:e_1\ldots k_n :e_n\}$ is $Map<\DYNAMIC{}, \DYNAMIC{}>$. 3092 The static type of a map literal of the form \CONST{}$ <K, V>\{k_1:e_1\ldots k_ n :e_n\}$ or the form $<K, V>\{k_1:e_1\ldots k_n :e_n\}$ is $Map<K, V>$. The sta tic type a map literal of the form \CONST{}$\{k_1:e_1\ldots k_n :e_n\}$ or the form $\{k_1:e_1\ldots k_n :e_n\}$ is $Map<\DYNAMIC{}, \DYNAMIC{}>$.
3028 3093
3029 3094
3030 \subsection{Throw} 3095 \subsection{Throw}
3031 \LMLabel{throw} 3096 \LMLabel{throw}
3032 3097
3033 \LMHash{} 3098 \LMHash{}
3034 The {\em throw expression} is used to raise an exception. 3099 The {\em throw expression} is used to throw an exception.
3035 3100
3036 \begin{grammar} 3101 \begin{grammar}
3037 {\bf throwExpression:} 3102 {\bf throwExpression:}
3038 \THROW{} expression 3103 \THROW{} expression
3039 . 3104 .
3040 3105
3041 {\bf throwExpressionWithoutCascade:} 3106 {\bf throwExpressionWithoutCascade:}
3042 \THROW{} expressionWithoutCascade 3107 \THROW{} expressionWithoutCascade
3043 . 3108 .
3044 3109
3045 \end{grammar} 3110 \end{grammar}
3046 3111
3047 \LMHash{} 3112 \LMHash{}
3048 The {\em current exception} is the last exception raised and not subsequently c aught at a given moment during runtime. 3113 Evaluation of a throw expression of the form \code{\THROW{} $e$;} proceeds as f ollows:
3049 3114
3050 \LMHash{} 3115 \LMHash{}
3051 Evaluation of a throw expression of the form \code{\THROW{} $e$;} proceeds as follows: 3116 The expression $e$ is evaluated producing a value $v$.
3052
3053 \LMHash{}
3054 The expression $e$ is evaluated yielding a value $v$.
3055 3117
3056 \commentary{ 3118 \commentary{
3057 There is no requirement that the expression $e$ evaluate to a special kind of ex ception or error object. 3119 There is no requirement that the expression $e$ evaluate to a special kind of ex ception or error object.
3058 } 3120 }
3059 3121
3060 \LMHash{} 3122 \LMHash{}
3061 If $e$ evaluates to \NULL{} (\ref{null}), then a \code{NullThrownError} is throw n. Otherwise the current exception is set to $v$ and the current return value (\ ref{return}) becomes undefined. 3123 If $v$ is the null value (\ref{null}), then a \code{NullThrownError} is thrown. Otherwise let $t$ be a stack trace corresponding to the current execution state, and the \THROW{} statement {\em throws} (\ref{completion}) with $v$ as exceptio n object and $t$ as stack trace.
3062
3063 \rationale{The current exception and the current return value must never be simu ltaneously defined, as they represent mutually exclusive options for exiting the current function.
3064 }
3065 3124
3066 \LMHash{} 3125 \LMHash{}
3067 Let $f$ be the immediately enclosing function. 3126 If $v$ is an instance of class \code{Error} or a subclass thereof,
3068 3127 and it is the first time that \code{Error} object is thrown,
3069 \LMHash{} 3128 the stack trace $t$ is stored on $v$ so that it will be returned by the $v$'s \c ode{stackTrace} getter
3070 If $f$ is synchronous (\ref{functions}), control is transferred to the nearest d ynamically enclosing exception handler.
3071 3129
3072 \commentary{ 3130 \commentary{
3073 If $f$ is marked \SYNC* then a dynamically enclosing exception handler encloses the call to \code{moveNext()} that initiated the evaluation of the throw express ion. 3131 If the same \code{Error} object is thrown more than once, its \code{stackTrace} getter will return the stack trace from the {\em first} time it was thrown.
3074 } 3132 }
3075 3133
3076 \LMHash{} 3134 \LMHash{}
3077 If $f$ is asynchronous then if there is a dynamically enclosing exception handl er $h$ (\ref{try}) introduced by the current activation, control is transferred to $h$, otherwise $f$ terminates.
3078
3079 \rationale{
3080 The rules for where a thrown exception will be handled must necessarily differ b etween the synchronous and asynchronous cases. Asynchronous functions cannot tra nsfer control to an exception handler defined outside themselves. Asynchronous generators post exceptions to their stream. Other asynchronous functions report exceptions via their future.
3081 }
3082
3083 \LMHash{}
3084 If the object being thrown is an instance of class \code{Error} or a subclass th ereof, its \code{stackTrace} getter will return the stack trace current at the p oint where the object was first thrown.
3085
3086 \LMHash{}
3087 The static type of a throw expression is $\bot$. 3135 The static type of a throw expression is $\bot$.
3088 3136
3089 3137
3090 \subsection{ Function Expressions} 3138 \subsection{ Function Expressions}
3091 \LMLabel{functionExpressions} 3139 \LMLabel{functionExpressions}
3092 3140
3093 \LMHash{} 3141 \LMHash{}
3094 A {\em function literal} is an object that encapsulates an executable unit of co de. 3142 A {\em function literal} is an object that encapsulates an executable unit of co de.
3095 3143
3096 \begin{grammar} 3144 \begin{grammar}
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
3338 First, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k} : a_{n+k})$ is evaluated. 3386 First, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k} : a_{n+k})$ is evaluated.
3339 3387
3340 \LMHash{} 3388 \LMHash{}
3341 If $T$ is a deferred type with prefix $p$, then if $p$ has not been successfully loaded, a dynamic error occurs. 3389 If $T$ is a deferred type with prefix $p$, then if $p$ has not been successfully loaded, a dynamic error occurs.
3342 3390
3343 \LMHash{} 3391 \LMHash{}
3344 Then, if $q$ is a non-factory constructor of an abstract class then an \code{Abs tractClassInstantiationError} is thrown. 3392 Then, if $q$ is a non-factory constructor of an abstract class then an \code{Abs tractClassInstantiationError} is thrown.
3345 3393
3346 \LMHash{} 3394 \LMHash{}
3347 If $T$ is malformed or if $T$ is a type variable a dynamic error occurs. In che cked mode, if $T$ or any of its superclasses is malbounded a dynamic error occur s. 3395 If $T$ is malformed or if $T$ is a type variable a dynamic error occurs. In che cked mode, if $T$ or any of its superclasses is malbounded a dynamic error occur s.
3348 Otherwise, if $q$ is not defined or not accessible, a \code{NoSuchMethodError} is thrown. If $q$ has less than $n$ positional parameters or more than $n$ req uired parameters, or if $q$ lacks any of the keyword parameters $\{ x_{n+1}, \ld ots, x_{n+k}\}$ a \code{NoSuchMethodError} is thrown. 3396 Otherwise, if $q$ is not defined or not accessible, a \code{NoSuchMethodError} i s thrown. If $q$ has fewer than $n$ positional parameters or more than $n$ requ ired parameters, or if $q$ lacks any of the keyword parameters $\{ x_{n+1}, \ldo ts, x_{n+k}\}$ a \code{NoSuchMethodError} is thrown.
3349 3397
3350 \LMHash{} 3398 \LMHash{}
3351 Otherwise, if $q$ is a generative constructor (\ref{generativeConstructors}), th en: 3399 Otherwise, if $q$ is a generative constructor (\ref{generativeConstructors}), th en:
3352 3400
3353 \commentary{Note that it this point we are assured that the number of actual typ e arguments match the number of formal type parameters.} 3401 \commentary{Note that it this point we are assured that the number of actual typ e arguments match the number of formal type parameters.}
3354 3402
3355 \LMHash{} 3403 \LMHash{}
3356 A fresh instance (\ref{generativeConstructors}), $i$, of class $R$ is allocated . For each instance variable $f$ of $i$, if the variable declaration of $f$ has an initializer expression $e_f$, then $e_f$ is evaluated, with the type paramet ers (if any) of $R$ bound to the actual type arguments $V_1, \ldots, V_l$, to an object $o_f$ and $f$ is bound to $o_f$. Otherwise $f$ is bound to \NULL{}. 3404 A fresh instance (\ref{generativeConstructors}), $i$, of class $R$ is allocated.
3405 Then $q$ is executed to initialize $i$ with its formal parameters bound to the e valuated argument list and, if $R$ is a generic class, with its type parameters bound to $V_1 \ldots V_m$.
3357 3406
3358 \commentary{ 3407 If execution of $q$ completes normally, $e$ evaluates to $i$.
3359 Observe that \THIS{} is not in scope in $e_f$. Hence, the initialization cannot depend on other properties of the object being instantiated.
3360 }
3361
3362 \LMHash{}
3363 Next, $q$ is executed with \THIS{} bound to $i$, the type parameters (if any) of $R$ bound to the actual type arguments $V_1, \ldots, V_l$ and the formal par ameter bindings that resulted from the evaluation of the argument list. The resu lt of the evaluation of $e$ is $i$.
3364 3408
3365 \LMHash{} 3409 \LMHash{}
3366 Otherwise, $q$ is a factory constructor (\ref{factories}). Then: 3410 Otherwise, $q$ is a factory constructor (\ref{factories}). Then:
3367 3411
3368 \LMHash{} 3412 \LMHash{}
3369 If $q$ is a redirecting factory constructor of the form $T(p_1, \ldots, p_{n+k}) = c;$ or of the form $T.id(p_1, \ldots, p_{n+k}) = c;$ then the result of the evaluation of $e$ is equivalent to evaluating the expression 3413 If $q$ is a redirecting factory constructor of the form $T(p_1, \ldots, p_{n+k}) = c;$ or of the form $T.id(p_1, \ldots, p_{n+k}) = c;$ then the result of the evaluation of $e$ is equivalent to evaluating the expression
3370 3414
3371 $[V_1, \ldots, V_m/T_1, \ldots, T_m]($\code{\NEW{} $c(a_1, \ldots, a_n, x_{n+1 }: a_{n+1}, \ldots, x_{n+k}: a_{n+k}))$}. If evaluation of $q$ causes $q$ to be re-evaluated cyclically, a runtime error occurs. 3415 $[V_1, \ldots, V_m/T_1, \ldots, T_m]($\code{\NEW{} $c(a_1, \ldots, a_n, x_{n+1 }: a_{n+1}, \ldots, x_{n+k}: a_{n+k}))$}. If evaluation of $q$ causes $q$ to be re-evaluated cyclically, with only factory constructor redirections in-between, a runtime error occurs.
3416 % Used to not have the "in-between" clause, which would disallow a factory const ructor redirecting to another constructor which conditionally calls the original factory constructor again with different arguments.
3372 3417
3373 3418
3374 \LMHash{} 3419 \LMHash{}
3375 Otherwise, the body of $q$ is executed with respect to the bindings that resulte d from the evaluation of the argument list and the type parameters (if any) of $ q$ bound to the actual type arguments $V_1, \ldots, V_l$ resulting in an object $i$. The result of the evaluation of $e$ is $i$. 3420 Otherwise, the body of $q$ is executed with respect to the bindings that resulte d from the evaluation of the argument list and the type parameters (if any) of $ q$ bound to the actual type arguments $V_1, \ldots, V_l$.
3421 If this execution {\em returns a value} (\ref{completion}),
3422 let $i$ be the returned value.
3423 If the execution {\em completes normally} or {\em returns with no value}, let $i $ be \code{null}.
3424 The result of the evaluation of $e$ is $i$.
3376 3425
3377 \LMHash{} 3426 \LMHash{}
3378 It is a static warning if $q$ is a constructor of an abstract class and $q$ is n ot a factory constructor. 3427 It is a static warning if $q$ is a constructor of an abstract class and $q$ is n ot a factory constructor.
3379 3428
3380 \commentary{The above gives precise meaning to the idea that instantiating an ab stract class leads to a warning. 3429 \commentary{The above gives precise meaning to the idea that instantiating an ab stract class leads to a warning.
3381 A similar clause applies to constant object creation in the next section. 3430 A similar clause applies to constant object creation in the next section.
3382 } 3431 }
3383 3432
3384 \rationale{In particular, a factory constructor can be declared in an abstract c lass and used safely, as it will either produce a valid instance or lead to a wa rning inside its own declaration. 3433 \rationale{In particular, a factory constructor can be declared in an abstract c lass and used safely, as it will either produce a valid instance or lead to a wa rning inside its own declaration.
3385 } 3434 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
3529 3578
3530 3579
3531 3580
3532 \subsection{ Function Invocation} 3581 \subsection{ Function Invocation}
3533 \LMLabel{functionInvocation} 3582 \LMLabel{functionInvocation}
3534 3583
3535 \LMHash{} 3584 \LMHash{}
3536 Function invocation occurs in the following cases: when a function expression ( \ref{functionExpressions}) is invoked (\ref{functionExpressionInvocation}), when a method (\ref{methodInvocation}), getter (\ref{topLevelGetterInvocation}, \ref {propertyExtraction}) or setter (\ref{assignment}) is invoked or when a construc tor is invoked (either via instance creation (\ref{instanceCreation}), construct or redirection (\ref{redirectingConstructors}) or super initialization). The var ious kinds of function invocation differ as to how the function to be invoked, $ f$, is determined, as well as whether \THIS{} (\ref{this}) is bound. Once $f$ h as been determined, the formal parameters of $f$ are bound to corresponding actu al arguments. When the body of $f$ is executed it will be executed with the afor ementioned bindings. 3585 Function invocation occurs in the following cases: when a function expression ( \ref{functionExpressions}) is invoked (\ref{functionExpressionInvocation}), when a method (\ref{methodInvocation}), getter (\ref{topLevelGetterInvocation}, \ref {propertyExtraction}) or setter (\ref{assignment}) is invoked or when a construc tor is invoked (either via instance creation (\ref{instanceCreation}), construct or redirection (\ref{redirectingConstructors}) or super initialization). The var ious kinds of function invocation differ as to how the function to be invoked, $ f$, is determined, as well as whether \THIS{} (\ref{this}) is bound. Once $f$ h as been determined, the formal parameters of $f$ are bound to corresponding actu al arguments. When the body of $f$ is executed it will be executed with the afor ementioned bindings.
3537 3586
3538 \LMHash{} 3587 \LMHash{}
3539 If $f$ is marked \ASYNC{} (\ref{functions}), then a fresh instance (\ref{generat iveConstructors}) $o$ implementing the built-in class \code{Future} is associate d with the invocation and immediately returned to the caller. The body of $f$ is scheduled for execution at some future time. The future $o$ will complete when $f$ terminates. The value used to complete $o$ is the current return value (\ref {return}), if it is defined, and the current exception (\ref{throw}) otherwise. 3588 If $f$ is synchronous and is not a generator (\ref{functions}) then execution of the body of $f$ begins immediately.
3589 If that execution {\em returns} a value, $v$, (\ref{completion}), the invocation evaluates to $v$.
3590 If it {\em completes normally} or {\em returns without a value}, the invocation evaluates to \code{null} (\ref{null}).
3591 If it {\em throws}, the invocation throws the same exception object and stack tr ace.
3540 3592
3541 \LMHash{} 3593 \commentary{
3542 If $f$ is marked \ASYNC* (\ref{functions}), then a fresh instance $s$ implementi ng the built-in class \code{Stream} is associated with the invocation and immedi ately returned. When $s$ is listened to, execution of the body of $f$ will begin . When $f$ terminates: 3594 A function body can never complete by {\em breaking} or {\em continuing}
3543 \begin{itemize} 3595 because any \BREAK{} or \CONTINUE{} statement must occur inside a statement
3544 \item If the current return value is defined then, if $s$ has been canceled then its cancellation future is completed with \NULL{} (\ref{null}). 3596 which will handle the \BREAK{} or \CONTINUE{},
3545 \item If the current exception $x$ is defined: 3597 either by declaring the same label, if the \BREAK{} or \CONTINUE{} has a label,
3546 \begin{itemize} 3598 or merely by being a loop or \SWITCH{} statement.
3547 \item $x$ is added to $s$. 3599 This means that a function body can only either {\em complete normally}, {\em th row}, or {\em return} a value. Completing normally is treated the same as return ing \code{null}, so a function call expression can always propagate the behavior of the called function's body.
3548 \item If $s$ has been canceled then its cancellation future is completed with $x$ as an error.
3549 \end{itemize}
3550 \item $s$ is closed.
3551 \end{itemize}
3552
3553 \rationale{
3554 When an asynchronous generator's stream has been canceled, cleanup will occur in the \FINALLY{} clauses (\ref{try}) inside the generator. We choose to direct an y exceptions that occur at this time to the cancellation future rather than have them be lost.
3555 } 3600 }
3556 3601
3557 \LMHash{}
3558 If $f$ is asynchronous then, when $f$ terminates, any open stream subscriptions associated with any asynchronous for loops (\ref{asynchronousFor-in}) or yield- each statements (\ref{yieldEach}) executing within $f$ are canceled, in the ord er of their nesting, innermost first.
3559
3560 \rationale{Such streams may be left open by for loops that were escaped when an exception was thrown within them for example.
3561 }
3562
3563 %\LMHash{}
3564 %When a stream is canceled, the implementation must wait for the cancelation fut ure returned by \cd{cancell()} to complete before proceeding.
3565 3602
3566 \LMHash{} 3603 \LMHash{}
3567 If $f$ is marked \SYNC* (\ref{functions}), then a fresh instance $i$ implementin g the built-in class \code{Iterable} is associated with the invocation and immed iately returned. 3604 If $f$ is marked \SYNC* (\ref{functions}), then a fresh instance $i$ implementin g the built-in class \code{Iterable} is associated with the invocation and immed iately returned.
3568 3605
3569
3570 \commentary{ 3606 \commentary{
3571 A Dart implementation will need to provide a specific implementation of \code{It erable} that will be returned by \SYNC* methods. A typical strategy would be to produce an instance of a subclass of class \code{IterableBase} defined in \code{ dart:core}. The only method that needs to be added by the Dart implementation in that case is \code{iterator}. 3607 A Dart implementation will need to provide a specific implementation of \code{It erable} that will be returned by \SYNC* methods. A typical strategy would be to produce an instance of a subclass of class \code{IterableBase} defined in \code{ dart:core}. The only method that needs to be added by the Dart implementation in that case is \code{iterator}.
3572 } 3608 }
3573 3609
3574 \LMHash{} 3610 \LMHash{}
3575 The iterable implementation must comply with the contract of \code{Iterable} and should not take any steps identified as exceptionally efficient in that contrac t. 3611 The iterable implementation must comply with the contract of \code{Iterable} and should not take any steps identified as exceptionally efficient in that contrac t.
3576 3612
3577 \commentary { 3613 \commentary {
3578 The contract explicitly mentions a number of situations where certain iterables could be more efficient than normal. For example, by precomputing their length. Normal iterables must iterate over their elements to determine their length. Thi s is certainly true in the case of a synchronous generator, where each element i s computed by a function. It would not be acceptable to pre-compute the results of the generator and cache them, for example. 3614 The contract explicitly mentions a number of situations where certain iterables could be more efficient than normal. For example, by precomputing their length. Normal iterables must iterate over their elements to determine their length. Thi s is certainly true in the case of a synchronous generator, where each element i s computed by a function. It would not be acceptable to pre-compute the results of the generator and cache them, for example.
3579 } 3615 }
3580 3616
3581 \LMHash{} 3617 \LMHash{}
3582 When iteration over the iterable is started, by getting an iterator $j$ from the iterable and calling \code{moveNext()}, execution of the body of $f$ will begin . When $f$ terminates, $j$ is positioned after its last element, so that its cur rent value is \NULL{} and the current call to \code{moveNext()} on $j$ returns f alse, as will all further calls. 3618 When iteration over the iterable is started, by getting an iterator $j$ from the iterable and calling \code{moveNext()}, execution of the body of $f$ will begin . When execution of the body of $f$ completes (\ref{completion},
3619 \begin{itemize}
3620 \item If it {\em returns} or {\em completes normally}, $j$ is positioned after i ts last element, so that its current value is \code{null} and the current call t o \code{moveNext()} on $j$ returns false, as must all further calls.
3621 \item If it {\em throws} exception $e$ and stack trace $t$ then the current valu e of $j$ is \code{null} and the current call to \code{moveNext()} throws $e$ and $t$ as well. Further calls to \code{moveNext()} must return false.
3622 \end{itemize}
3583 3623
3584 Each iterator starts a separate computation. If the \SYNC* function is impure, t he sequence of values yielded by each iterator may differ. 3624 Each iterator starts a separate computation. If the \SYNC* function is impure, t he sequence of values yielded by each iterator may differ.
3585 3625
3586 \commentary{ 3626 \commentary{
3587 One can derive more than one iterator from a given iterable. Note that operati ons on the iterable itself can create distinct iterators. An example would be \c ode{length}. It is conceivable that different iterators might yield sequences o f different length. The same care needs to be taken when writing \SYNC* function s as when 3627 One can derive more than one iterator from a given iterable. Note that operati ons on the iterable itself can create distinct iterators. An example would be \c ode{length}. It is conceivable that different iterators might yield sequences o f different length. The same care needs to be taken when writing \SYNC* function s as when
3588 writing an \code{Iterator} class. In particular, it should handle multiple 3628 writing an \code{Iterator} class. In particular, it should handle multiple
3589 simultaneous iterators gracefully. If the iterator depends on external state 3629 simultaneous iterators gracefully. If the iterator depends on external state
3590 that might change, it should check that the state is still valid after every 3630 that might change, it should check that the state is still valid after every
3591 yield (and maybe throw a \code{ConcurrentModificationError} if it isn't). 3631 yield (and maybe throw a \code{ConcurrentModificationError} if it isn't).
3592 } 3632 }
3593 3633
3594 \LMHash{} 3634 \LMHash{}
3595 Each iterator runs with its own shallow copies of all local variables; in partic ular, each iterator has the same initial arguments, even if their bindings are m odified by the function. 3635 Each iterator runs with its own shallow copies of all local variables; in partic ular, each iterator has the same initial arguments, even if their bindings are m odified by the function.
3596 \commentary{ 3636 \commentary{
3597 Two executions of an iterator interact only via state outside the function. 3637 Two executions of an iterator interact only via state outside the function.
3598 } 3638 }
3599 % The alternative would be to cache the results of an iterator in the iterable, and check the cache at each \YIELD{}. This would have strange issues as well. T he yielded value might differ from the expression in the yield. And it is a pote ntial memory leak as the cache is kept alive by any iterator. 3639 % The alternative would be to cache the results of an iterator in the iterable, and check the cache at each \YIELD{}. This would have strange issues as well. T he yielded value might differ from the expression in the yield. And it is a pote ntial memory leak as the cache is kept alive by any iterator.
3600 3640
3641 \LMHash{}
3642 If $f$ is marked \ASYNC{} (\ref{functions}), then a fresh instance (\ref{generat iveConstructors}) $o$ implementing the built-in class \code{Future} is associate d with the invocation and immediately returned to the caller. The body of $f$ is scheduled for execution at some future time. The future $o$ will be completed w hen execution of the body of $f$ completes (\ref{completion}). If it {\em return s} a value, $o$ is completed with that value, if it {\em completes normally} or {\em returns with no value}, $o$ is completed with the value \code{null}, and if it completes by {\em throwing} an exception $e$ and stack trace $t$, $o$ is com pleted with the error $e$ and stack trace $t$.
3601 3643
3602 \LMHash{} 3644 \LMHash{}
3603 If $f$ is synchronous and is not a generator (\ref{functions}) then execution of the body of $f$ begins immediately. When $f$ terminates the current return val ue is returned to the caller. 3645 If $f$ is marked \ASYNC* (\ref{functions}), then a fresh instance $s$ implementi ng the built-in class \code{Stream} is associated with the invocation and immedi ately returned. When $s$ is listened to, execution of the body of $f$ will begin .
3646 When execution of the body of $f$ completes:
3647 \begin{itemize}
3648 \item If it {\em completes normally} or {\em returns with no value}, then if $s$ has been canceled then its cancellation future is completed with \code{null} (\ ref{null}).
3649 \item If it {\em throws} with exception $e$ and stack trace $t$:
3650 \begin{itemize}
3651 \item If $s$ has been canceled then its cancellation future is completed with error $e$ and stack trace $t$.
3652 \item otherwise the error $e$ and stack trace $t$ are emitted by $s$.
3653 \end{itemize}
3654 \item $s$ is closed.
3655 \end{itemize}
3656 \commentary{
3657 The body of an asynchronous generator function cannot {\em break}, {\em continue } or {\em return a value}. The first two are only allowed in contexts that will handle the break or continue, and return statements with an expression are not a llowed in generator functions.
3658 }
3604 3659
3660 \rationale{
3661 When an asynchronous generator's stream has been canceled, cleanup will occur in the \FINALLY{} clauses (\ref{try}) inside the generator. We choose to direct an y exceptions that occur at this time to the cancellation future rather than have them be lost.
3662 }
3605 3663
3606 \LMHash{} 3664 %\LMHash{}
3607 Execution of $f$ terminates when the first of the following occurs: 3665 %When a stream is canceled, the implementation must wait for the cancelation fut ure returned by \cd{cancell()} to complete before proceeding.
3608 \begin{itemize}
3609 \item An exception is thrown and not caught within the current function activati on.
3610 \item A return statement (\ref{return}) immediately nested in the body of $f$ is executed and not intercepted in a \FINALLY{} (\ref{try}) clause.
3611 \item The last statement of the body completes execution.
3612 \end{itemize}
3613
3614
3615
3616 3666
3617 \subsubsection{ Actual Argument List Evaluation} 3667 \subsubsection{ Actual Argument List Evaluation}
3618 \LMLabel{actualArguments} 3668 \LMLabel{actualArguments}
3619 3669
3620 \LMHash{} 3670 \LMHash{}
3621 Function invocation involves evaluation of the list of actual arguments to the f unction and binding of the results to the function's formal parameters. 3671 Function invocation involves evaluation of the list of actual arguments to the f unction and binding of the results to the function's formal parameters.
3622 3672
3623 \begin{grammar} 3673 \begin{grammar}
3624 {\bf arguments:} 3674 {\bf arguments:}
3625 `(' (argumentList `,'?)? `)' 3675 `(' (argumentList `,'?)? `)'
(...skipping 14 matching lines...) Expand all
3640 Argument lists allow an optional trailing comma after the last argument ($`,'?$) . An argument list with such a trailing comma is equivalent in all ways to the s ame parameter list without the trailing comma. All argument lists in this specif ication are shown without a trailing comma, but the rules and semantics apply eq ually to the corresponding argument list with a trailing comma. 3690 Argument lists allow an optional trailing comma after the last argument ($`,'?$) . An argument list with such a trailing comma is equivalent in all ways to the s ame parameter list without the trailing comma. All argument lists in this specif ication are shown without a trailing comma, but the rules and semantics apply eq ually to the corresponding argument list with a trailing comma.
3641 3691
3642 \LMHash{} 3692 \LMHash{}
3643 Evaluation of an actual argument list of the form 3693 Evaluation of an actual argument list of the form
3644 3694
3645 $(a_1, \ldots, a_m, q_1: a_{m+1}, \ldots, q_l: a_{m+l})$ 3695 $(a_1, \ldots, a_m, q_1: a_{m+1}, \ldots, q_l: a_{m+l})$
3646 3696
3647 proceeds as follows: 3697 proceeds as follows:
3648 3698
3649 \LMHash{} 3699 \LMHash{}
3650 The arguments $a_1, \ldots, a_{m+l}$ are evaluated in the order they appear in t he program, yielding objects $o_1, \ldots, o_{m+l}$. 3700 The arguments $a_1, \ldots, a_{m+l}$ are evaluated in the order they appear in t he program, producing objects $o_1, \ldots, o_{m+l}$.
3651 3701
3652 \commentary{Simply stated, an argument list consisting of $m$ positional argumen ts and $l$ named arguments is evaluated from left to right. 3702 \commentary{Simply stated, an argument list consisting of $m$ positional argumen ts and $l$ named arguments is evaluated from left to right.
3653 } 3703 }
3654 3704
3655 3705
3656 \subsubsection{ Binding Actuals to Formals} 3706 \subsubsection{ Binding Actuals to Formals}
3657 \LMLabel{bindingActualsToFormals} 3707 \LMLabel{bindingActualsToFormals}
3658 3708
3659 \LMHash{} 3709 \LMHash{}
3660 Let $f$ be a function with $h$ required parameters, let $p_1 \ldots p_n$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+k}$ be the optional parameters declared by $f$. 3710 Let $f$ be a function with $h$ required parameters, let $p_1 \ldots p_n$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+k}$ be the optional parameters declared by $f$.
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
3833 $o.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$. 3883 $o.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$.
3834 3884
3835 \LMHash{} 3885 \LMHash{}
3836 Evaluation of an unconditional ordinary method invocation $i$ of the form 3886 Evaluation of an unconditional ordinary method invocation $i$ of the form
3837 3887
3838 $o.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ 3888 $o.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$
3839 3889
3840 proceeds as follows: 3890 proceeds as follows:
3841 3891
3842 \LMHash{} 3892 \LMHash{}
3843 First, the expression $o$ is evaluated to a value $v_o$. Next, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ is evaluated yielding actual argument objects $o_1, \ldots , o_{n+k}$. Let $f$ be the result of looking up (\ref{methodLookup}) method $m$ in $v_o$ with respect to the cur rent library $L$. 3893 First, the expression $o$ is evaluated to a value $v_o$. Next, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$ is evaluated producing actual argument objects $o_1, \ldots , o_{n+k}$. Let $f$ be the resul t of looking up (\ref{methodLookup}) method $m$ in $v_o$ with respect to the cu rrent library $L$.
3844 3894
3845 \LMHash{} 3895 \LMHash{}
3846 Let $p_1 \ldots p_h$ be the required parameters of $f$, let $p_1 \ldots p_m$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+l}$ be the opti onal parameters declared by $f$. 3896 Let $p_1 \ldots p_h$ be the required parameters of $f$, let $p_1 \ldots p_m$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+l}$ be the opti onal parameters declared by $f$.
3847 3897
3848 \commentary{ 3898 \commentary{
3849 We have an argument list consisting of $n$ positional arguments and $k$ named ar guments. We have a function with $h$ required parameters and $l$ optional parame ters. The number of positional arguments must be at least as large as the number of required parameters, and no larger than the number of positional parameters. All named arguments must have a corresponding named parameter. 3899 We have an argument list consisting of $n$ positional arguments and $k$ named ar guments. We have a function with $h$ required parameters and $l$ optional parame ters. The number of positional arguments must be at least as large as the number of required parameters, and no larger than the number of positional parameters. All named arguments must have a corresponding named parameter.
3850 } 3900 }
3851 3901
3852 \LMHash{} 3902 \LMHash{}
3853 If $n < h$, or $n > m$, the method lookup has failed. Furthermore, each $x_i, n +1 \le i \le n+k$, must have a corresponding named parameter in the set $\{p_{m +1}, \ldots, p_{h+l}\}$ or the method lookup also fails. If $v_o$ is an instanc e of \code{Type} but $o$ is not a constant type literal, then if $m$ is a method that forwards (\ref{functionDeclarations}) to a static method, method lookup fa ils. Otherwise method lookup has succeeded. 3903 If $n < h$, or $n > m$, the method lookup has failed. Furthermore, each $x_i, n +1 \le i \le n+k$, must have a corresponding named parameter in the set $\{p_{m +1}, \ldots, p_{h+l}\}$ or the method lookup also fails. If $v_o$ is an instanc e of \code{Type} but $o$ is not a constant type literal, then if $m$ is a method that forwards (\ref{functionDeclarations}) to a static method, method lookup fa ils. Otherwise method lookup has succeeded.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
3942 3992
3943 \LMHash{} 3993 \LMHash{}
3944 A super method invocation $i$ has the form 3994 A super method invocation $i$ has the form
3945 3995
3946 $\SUPER{}.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$. 3996 $\SUPER{}.m(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k}: a_{n+k})$.
3947 3997
3948 \LMHash{} 3998 \LMHash{}
3949 Evaluation of $i$ proceeds as follows: 3999 Evaluation of $i$ proceeds as follows:
3950 4000
3951 \LMHash{} 4001 \LMHash{}
3952 First, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k} : a_{n+k})$ is evaluated yielding actual argument objects $o_1, \ldots , o_{n+k }$. Let $g$ be the method currently executing, and let $C$ be the class in which $g$ was looked up (\ref{methodLookup}). Let $S_{dynamic}$ be the superclass of $C$, and let $f$ be the result of looking up method (\ref{methodLookup}) $m$ in $S_{dynamic}$ with respect to the current library $L$. 4002 First, the argument list $(a_1, \ldots , a_n, x_{n+1}: a_{n+1}, \ldots , x_{n+k} : a_{n+k})$ is evaluated producing actual argument objects $o_1, \ldots , o_{n+k }$. Let $g$ be the method currently executing, and let $C$ be the class in which $g$ was looked up (\ref{methodLookup}). Let $S_{dynamic}$ be the superclass of $C$, and let $f$ be the result of looking up method (\ref{methodLookup}) $m$ in $S_{dynamic}$ with respect to the current library $L$.
3953 Let $p_1 \ldots p_h$ be the required parameters of $f$, let $p_1 \ldots p_m$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+l}$ be the opti onal parameters declared by $f$. 4003 Let $p_1 \ldots p_h$ be the required parameters of $f$, let $p_1 \ldots p_m$ be the positional parameters of $f$ and let $p_{h+1}, \ldots, p_{h+l}$ be the opti onal parameters declared by $f$.
3954 4004
3955 \LMHash{} 4005 \LMHash{}
3956 If $n < h$, or $n > m$, the method lookup has failed. Furthermore, each $x_i, n +1 \le i \le n+k$, must have a corresponding named parameter in the set $\{p_{m +1}, \ldots, p_{h+l}\}$ or the method lookup also fails. Otherwise method looku p has succeeded. 4006 If $n < h$, or $n > m$, the method lookup has failed. Furthermore, each $x_i, n +1 \le i \le n+k$, must have a corresponding named parameter in the set $\{p_{m +1}, \ldots, p_{h+l}\}$ or the method lookup also fails. Otherwise method looku p has succeeded.
3957 4007
3958 \LMHash{} 4008 \LMHash{}
3959 If the method lookup succeeded, the body of $f$ is executed with respect to the bindings that resulted from the evaluation of the argument list, and with \THIS{ } bound to the current value of \THIS{}. The value of $i$ is the value returned after $f$ is executed. 4009 If the method lookup succeeded, the body of $f$ is executed with respect to the bindings that resulted from the evaluation of the argument list, and with \THIS{ } bound to the current value of \THIS{}. The value of $i$ is the value returned after $f$ is executed.
3960 4010
3961 \LMHash{} 4011 \LMHash{}
3962 If the method lookup has failed, then let $g$ be the result of looking up getter (\ref{getterAndSetterLookup}) $m$ in $S_{dynamic}$ with respect to $L$. If the getter lookup succeeded, let $v_g$ be the value of the getter invocation $\SUPER {}.m$. Then the value of $i$ is the result of invoking 4012 If the method lookup has failed, then let $g$ be the result of looking up getter (\ref{getterAndSetterLookup}) $m$ in $S_{dynamic}$ with respect to $L$. If the getter lookup succeeded, let $v_g$ be the value of the getter invocation $\SUPER {}.m$. Then the value of $i$ is the result of invoking
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
4712 {\bf logicalAndExpression:} 4762 {\bf logicalAndExpression:}
4713 equalityExpression (`\&\&' equalityExpression)* 4763 equalityExpression (`\&\&' equalityExpression)*
4714 % bitwiseOrExpression (`\&\&' bitwiseOrExpression)* 4764 % bitwiseOrExpression (`\&\&' bitwiseOrExpression)*
4715 . 4765 .
4716 \end{grammar} 4766 \end{grammar}
4717 4767
4718 \LMHash{} 4768 \LMHash{}
4719 A {\em logical boolean expression} is either an equality expression (\ref{equali ty}), or an invocation of a logical boolean operator on an expression $e_1$ with argument $e_2$. 4769 A {\em logical boolean expression} is either an equality expression (\ref{equali ty}), or an invocation of a logical boolean operator on an expression $e_1$ with argument $e_2$.
4720 4770
4721 \LMHash{} 4771 \LMHash{}
4722 Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes t he evaluation of $e_1$ which is then subjected to boolean conversion, yielding an object $o_1$; if $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE, other wise $e_2$ is evaluated to an object $o_2$, which is then subjected to boolean c onversion (\ref{booleanConversion}) producing an object $r$, which is the value of $b$. 4772 Evaluation of a logical boolean expression $b$ of the form $e_1 || e_2$ causes t he evaluation of $e_1$ which is then subjected to boolean conversion, producing an object $o_1$; if $o_1$ is \TRUE, the result of evaluating $b$ is \TRUE, othe rwise $e_2$ is evaluated to an object $o_2$, which is then subjected to boolean conversion (\ref{booleanConversion}) producing an object $r$, which is the value of $b$.
4723 4773
4724 \LMHash{} 4774 \LMHash{}
4725 Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$ causes the evaluation of $e_1$ which is then subjected to boolean conversion, yielding an object $o_1$; if $o_1$ is not \TRUE, the result of evaluating $b$ is \FALSE , otherwise $e_2$ is evaluated to an object $o_2$, which is then subjected to bo olean conversion producing an object $r$, which is the value of $b$. 4775 Evaluation of a logical boolean expression $b$ of the form $e_1 \&\& e_2$ causes the evaluation of $e_1$ which is then subjected to boolean conversion, producin g an object $o_1$; if $o_1$ is not \TRUE, the result of evaluating $b$ is \FALS E, otherwise $e_2$ is evaluated to an object $o_2$, which is then subjected to b oolean conversion producing an object $r$, which is the value of $b$.
4726 4776
4727 \LMHash{} 4777 \LMHash{}
4728 A logical boolean expression $b$ of the form $e_1 \&\& e_2$ shows that a variabl e $v$ has type 4778 A logical boolean expression $b$ of the form $e_1 \&\& e_2$ shows that a variabl e $v$ has type
4729 $T$ if all of the following conditions hold: 4779 $T$ if all of the following conditions hold:
4730 \begin{itemize} 4780 \begin{itemize}
4731 \item Either $e_1$ shows that $v$ has type $T$ or $e_2$ shows that $v$ has type $T$. 4781 \item Either $e_1$ shows that $v$ has type $T$ or $e_2$ shows that $v$ has type $T$.
4732 \item $v$ is a local variable or formal parameter. 4782 \item $v$ is a local variable or formal parameter.
4733 \item The variable $v$ is not mutated in $e_2$ or within a closure. 4783 \item The variable $v$ is not mutated in $e_2$ or within a closure.
4734 \end{itemize} 4784 \end{itemize}
4735 4785
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
5002 \LMHash{} 5052 \LMHash{}
5003 An {\em await expression} allows code to yield control until an asynchronous ope ration (\ref{functions}) completes. 5053 An {\em await expression} allows code to yield control until an asynchronous ope ration (\ref{functions}) completes.
5004 5054
5005 \begin{grammar} 5055 \begin{grammar}
5006 {\bf awaitExpression:} 5056 {\bf awaitExpression:}
5007 \AWAIT{} unaryExpression 5057 \AWAIT{} unaryExpression
5008 \end{grammar} 5058 \end{grammar}
5009 5059
5010 \LMHash{} 5060 \LMHash{}
5011 Evaluation of an await expression $a$ of the form \AWAIT{} $e$ proceeds as follo ws: 5061 Evaluation of an await expression $a$ of the form \AWAIT{} $e$ proceeds as follo ws:
5012 First, the expression $e$ is evaluated. Next: 5062 First, the expression $e$ is evaluated to an object $o$. Next:
5013 5063
5014 \LMHash{} 5064 \LMHash{}
5015 If $e$ raises an exception $x$, then an instance $f$ of class \code{Future} is a llocated and later completed with $x$. Otherwise, if $e$ evaluates to an object $o$ that is not an instance of \code{Future}, then let $f$ be the result of call ing \code{Future.value()} with $o$ as its argument; otherwise let $f$ be the res ult of evaluating $e$. 5065 % NOTICE: Removed the requirement that an error thrown by $e$ is caught in a
5066 % future. There is no reason $var x = e; await x;$ and $await e$ should behave
5067 % differently, and no implementation actually implemented it.
5068 If $o$ is not an instance of \code{Future}, then let $f$ be the result of creati ng a new object using the constructor \code{Future.value()} with $o$ as its argu ment; otherwise let $f$ be $o$.
5016 5069
5017 \LMHash{} 5070 \LMHash{}
5018 Next, execution of the function $m$ immediately enclosing $a$ is suspended unti l after $f$ completes. The stream associated with the innermost enclosing asynch ronous for loop (\ref{asynchronousFor-in}), if any, is paused. At some time afte r $f$ is completed, control returns to the current invocation. The stream associ ated with the innermost enclosing asynchronous for loop (\ref{asynchronousFor-i n}), if any, is resumed. If $f$ has completed with an exception $x$, $a$ raises $x$. If $f$ completes with a value $v$, $a$ evaluates to $v$. 5071 Next, the stream associated with the innermost enclosing asynchronous for loop ( \ref{asynchronousFor-in}), if any, is paused. The current invocation of the func tion body immediately enclosing $a$ is suspended until after $f$ completes. At s ome time after $f$ is completed, control returns to the current invocation. If $ f$ has completed with an error $x$ and stack trace $t$, $a$ {\em throws} $x$ and $t$. If $f$ completes with a value $v$, $a$ evaluates to $v$.
5019 5072
5020 %Otherwise, the value of $a$ is the value of $e$. If evaluation of $e$ raises an exception $x$, $a$ raises $x$. 5073 %Otherwise, the value of $a$ is the value of $e$. If evaluation of $e$ raises an exception $x$, $a$ raises $x$.
5021 5074
5022 \commentary{ 5075 \commentary{
5023 It is a compile-time error if the function immediately enclosing $a$ is not declared asynchronous. However, this error is simply a syntax error, because in the context of a normal function, \AWAIT{} has no special meaning. 5076 It is a compile-time error if the function immediately enclosing $a$ is not declared asynchronous. However, this error is simply a syntax error, because in the context of a normal function, \AWAIT{} has no special meaning.
5077 % TODO(lrn): Update this, it's not actually correct,
5078 % the expression "await(expr)" is valid non-async syntax *and* a valid
5079 % async await expression.
5024 } 5080 }
5025 5081
5026 \rationale{ 5082 \rationale{
5027 An await expression has no meaning in a synchronous function. If such a function were to suspend waiting for a future, it would no longer be synchronous. 5083 An await expression has no meaning in a synchronous function. If such a function were to suspend waiting for a future, it would no longer be synchronous.
5028 } 5084 }
5029 5085
5030 \commentary{ 5086 \commentary{
5031 It is not a static warning if the type of $e$ is not a subtype of \code{Future}. Tools may choose to give a hint in such cases. 5087 It is not a static warning if the type of $e$ is not a subtype of \code{Future}. Tools may choose to give a hint in such cases.
5032 } 5088 }
5033 5089
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
5418 \LMHash{} 5474 \LMHash{}
5419 The expression $e$ is evaluated to a value $v$. Then, if $T$ is a malformed or d eferred type (\ref{staticTypes}), a dynamic error occurs. Otherwise, if the inte rface of the class of $v$ is a subtype of $T$, the cast expression evaluates to $v$. Otherwise, if $v$ is \NULL{}, the cast expression evaluates to $v$. 5475 The expression $e$ is evaluated to a value $v$. Then, if $T$ is a malformed or d eferred type (\ref{staticTypes}), a dynamic error occurs. Otherwise, if the inte rface of the class of $v$ is a subtype of $T$, the cast expression evaluates to $v$. Otherwise, if $v$ is \NULL{}, the cast expression evaluates to $v$.
5420 In all other cases, a \code{CastError} is thrown. 5476 In all other cases, a \code{CastError} is thrown.
5421 5477
5422 \LMHash{} 5478 \LMHash{}
5423 The static type of a cast expression \code{$e$ \AS{} $T$} is $T$. 5479 The static type of a cast expression \code{$e$ \AS{} $T$} is $T$.
5424 5480
5425 5481
5426 \section{Statements} 5482 \section{Statements}
5427 \LMLabel{statements} 5483 \LMLabel{statements}
5484 A {\em statement} is a fragment of Dart code that can be executed at runtime. St atements, unlike expressions, do not evaluate to a value, but are instead execut ed for their effect on the program state.
5485 Some statements or expression can affect control flow, in particular \BREAK{}, \ CONTINUE{}, \RETURN{}, \RETHROW{} and \THROW{}.
5486
5487 \LMLabel{completion}
5488 Execution of a statement can {\em complete} in one of five ways: either it {\em completes normally}, it {\em breaks} or it {\em continues} (either to a label or without a label), it {\em returns} (with or without a value), or it {\em throws } an exception object and an associated stack trace.
5489
5490 In the description of statement execution, the default is that the execution
5491 {\em completes normally} unless otherwise stated.
5492
5493 If the execution of a statement, $s$, is defined in terms of executing
5494 another statement,
5495 like the body of a loop or the branches of an \IF{} statement,
5496 and the execution of that other statement doesn't complete normally,
5497 then, unless otherwise stated, the execution of $s$ stops
5498 at that point and completes in the same way.
5499
5500 If the execution of a statement is defined in terms of evaluating an expression,
5501 like the condition expression of an \IF{} statement or the value of a \RETURN{},
5502 and the evaluation of that expression throws,
5503 then, unless otherwise stated, the execution of the statement stops
5504 at that point and throws the same exception object and stack trace.
5505
5506 \LMHash{}
5428 5507
5429 \begin{grammar} 5508 \begin{grammar}
5430 {\bf statements:} 5509 {\bf statements:}
5431 statement* 5510 statement*
5432 . 5511 .
5433 5512
5434 5513
5435 {\bf statement:} 5514 {\bf statement:}
5436 label* nonLabelledStatement 5515 label* nonLabelledStatement
5437 . 5516 .
(...skipping 15 matching lines...) Expand all
5453 expressionStatement; 5532 expressionStatement;
5454 assertStatement; 5533 assertStatement;
5455 localFunctionDeclaration 5534 localFunctionDeclaration
5456 . 5535 .
5457 \end{grammar} 5536 \end{grammar}
5458 5537
5459 \subsection{Blocks} 5538 \subsection{Blocks}
5460 \LMLabel{blocks} 5539 \LMLabel{blocks}
5461 5540
5462 \LMHash{} 5541 \LMHash{}
5463 A {\em block statement} supports sequencing of code. 5542 A {\em block statement} supports sequencing of code.
5464 5543
5465 \LMHash{} 5544 \LMHash{}
5466 Execution of a block statement $\{s_1, \ldots, s_n\}$ proceeds as follows: 5545 Execution of a block statement $\{s_1, \ldots, s_n\}$ proceeds as follows:
5467 5546
5468 \LMHash{} 5547 \LMHash{}
5469 For $i \in 1 .. n, s_i$ is executed. 5548 For $i \in 1 .. n, s_i$ is executed.
5470 5549
5471 \LMHash{} 5550 \LMHash{}
5472 A block statement introduces a new scope, which is nested in the lexically enclo sing scope in which the block statement appears. 5551 A block statement introduces a new scope, which is nested in the lexically enclo sing scope in which the block statement appears.
5473 5552
5474 5553 \subsection{Expression Statements}
5475 5554 \LMLabel{expressionStatements}
5476 \subsection{Expression Statements}
5477 \LMLabel{expressionStatements}
5478 5555
5479 \LMHash{} 5556 \LMHash{}
5480 An {\em expression statement} consists of an expression other than a non-constan t map literal (\ref{maps}) that has no explicit type arguments. 5557 An {\em expression statement} consists of an expression other than a non-constan t map literal (\ref{maps}) that has no explicit type arguments.
5481 5558
5482 \rationale{ 5559 \rationale{
5483 The restriction on maps is designed to resolve an ambiguity in the grammar, whe n a statement begins with \{. 5560 The restriction on maps is designed to resolve an ambiguity in the grammar, whe n a statement begins with \{.
5484 } 5561 }
5485 5562
5486 \begin{grammar} 5563 \begin{grammar}
5487 {\bf expressionStatement:} 5564 {\bf expressionStatement:}
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
5594 % elaborate on function identity and equality, runtime type. Likewsie in functio n expressions (closures) and declarations 5671 % elaborate on function identity and equality, runtime type. Likewsie in functio n expressions (closures) and declarations
5595 5672
5596 \subsection{If} 5673 \subsection{If}
5597 \LMLabel{if} 5674 \LMLabel{if}
5598 5675
5599 \LMHash{} 5676 \LMHash{}
5600 The {\em if statement} allows for conditional execution of statements. 5677 The {\em if statement} allows for conditional execution of statements.
5601 5678
5602 \begin{grammar} 5679 \begin{grammar}
5603 {\bf ifStatement:} 5680 {\bf ifStatement:}
5604 \IF{} `(' expression `)' statement ( \ELSE{} statement)? % we could allow top level expression 5681 \IF{} `(' expression `)' statement ( \ELSE{} statement)?
5605 . 5682 .
5606 \end{grammar} 5683 \end{grammar}
5607 5684
5608 Execution of an if statement of the form \code {\IF{} (}$b$\code{)}$s_1$ \code{ \ELSE{} } $s_2$ proceeds as follows: 5685 Execution of an if statement of the form \code {\IF{} (}$b$\code{)}$s_1$ \code{ \ELSE{} } $s_2$ proceeds as follows:
5609 5686
5610 \LMHash{} 5687 \LMHash{}
5611 First, the expression $b$ is evaluated to an object $o$. Then, $o$ is subjecte d to boolean conversion (\ref{booleanConversion}), producing an object $r$. If $ r$ is \TRUE{}, then the statement $\{s_1\}$ is executed, otherwise statement $\{ s_2\}$ is executed. 5688 First, the expression $b$ is evaluated to an object $o$. Then, $o$ is subjected to boolean conversion (\ref{booleanConversion}), producing an object $r$. If $r $ is \TRUE{}, then the statement $\{s_1\}$ is executed, otherwise statement $\{s _2\}$ is executed.
5612
5613 5689
5614 \commentary { 5690 \commentary {
5615 Put another way, \code {\IF{} (}$b$\code{)}$s_1$ \code{\ELSE{} } $s_2$ is equiv alent to 5691 Put another way, \code {\IF{} (}$b$\code{)}$s_1$ \code{\ELSE{} } $s_2$ is equiv alent to
5616 \code {\IF{} (}$b$\code{)}$\{s_1\}$ \code{\ELSE{} } $\{s_2\}$ 5692 \code {\IF{} (}$b$\code{)}$\{s_1\}$ \code{\ELSE{} } $\{s_2\}$
5617 } 5693 }
5618 5694
5619 \rationale { 5695 \rationale {
5620 The reason for this equivalence is to catch errors such as 5696 The reason for this equivalence is to catch errors such as
5621 } 5697 }
5622 \begin{dartCode} 5698 \begin{dartCode}
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
5684 \LMHash{} 5760 \LMHash{}
5685 If $c$ is empty then let $c^\prime$ be \TRUE{} otherwise let $c^\prime$ be $c$. 5761 If $c$ is empty then let $c^\prime$ be \TRUE{} otherwise let $c^\prime$ be $c$.
5686 5762
5687 \LMHash{} 5763 \LMHash{}
5688 First the variable declaration statement \VAR{} $v = e_0$ is executed. Then: 5764 First the variable declaration statement \VAR{} $v = e_0$ is executed. Then:
5689 \begin{enumerate} 5765 \begin{enumerate}
5690 \item 5766 \item
5691 \label{beginFor} 5767 \label{beginFor}
5692 If this is the first iteration of the for loop, let $v^\prime$ be $v$. Otherwise , let $v^\prime$ be the variable $v^{\prime\prime}$ created in the previous exe cution of step \ref{allocateFreshVar}. 5768 If this is the first iteration of the for loop, let $v^\prime$ be $v$. Otherwise , let $v^\prime$ be the variable $v^{\prime\prime}$ created in the previous exe cution of step \ref{allocateFreshVar}.
5693 \item 5769 \item
5694 The expression $[v^\prime/v]c$ is evaluated and subjected to boolean conversion (\ref{booleans}). If the result is \FALSE{}, the for loop completes. Otherwise, execution continues at step 5770 The expression $[v^\prime/v]c$ is evaluated and subjected to boolean conversion (\ref{booleans}). If the result is \FALSE{}, the for loop completes normally. Ot herwise, execution continues at step
5695 \ref{beginIteration}. 5771 \ref{beginIteration}.
5696 \item 5772 \item
5697 \label{beginIteration} 5773 \label{beginIteration}
5698 The statement $[v^\prime/v]\{s\}$ is executed. 5774 The statement $[v^\prime/v]\{s\}$ is executed.
5699 \item 5775
5776 If this execution {\em completes normally}, {\em continues} without a label,
5777 or {\em continues} with a label that prefixes this \FOR{} statement (\ref{labels }),
eernst 2016/10/19 13:37:05 Following the newest plan (around noon, Oct 19), t
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
5778 then execution of the statement is treated as if it had completed normally.
5779 If it completes in any other way,
5780 execution of the for loop stops and completes in the same way.
5781
5700 \label{allocateFreshVar} 5782 \label{allocateFreshVar}
5701 Let $v^{\prime\prime}$ be a fresh variable. $v^{\prime\prime}$ is bound to the value of $v^\prime$. 5783 Let $v^{\prime\prime}$ be a fresh variable. $v^{\prime\prime}$ is bound to the value of $v^\prime$.
5702 \item 5784 \item
5703 The expression $[v^{\prime\prime}/v]e$ is evaluated, and the process recurses at step 5785 The expression $[v^{\prime\prime}/v]e$ is evaluated, and the process recurses at step
5704 \ref{beginFor}. 5786 \ref{beginFor}.
5705 \end{enumerate} 5787 \end{enumerate}
5706 5788
5707 \rationale{ 5789 \rationale{
5708 The definition above is intended to prevent the common error where users create a closure inside a for loop, intending to close over the current binding of the loop variable, and find (usually after a painful process of debugging and learni ng) that all the created closures have captured the same value - the one current in the last iteration executed. 5790 The definition above is intended to prevent the common error where users create a closure inside a for loop, intending to close over the current binding of the loop variable, and find (usually after a painful process of debugging and learni ng) that all the created closures have captured the same value - the one current in the last iteration executed.
5709 5791
(...skipping 23 matching lines...) Expand all
5733 5815
5734 \begin{dartCode} 5816 \begin{dartCode}
5735 var n0 = $e$.iterator; 5817 var n0 = $e$.iterator;
5736 \WHILE{} (n0.moveNext()) \{ 5818 \WHILE{} (n0.moveNext()) \{
5737 $finalConstVarOrType?$ id = n0.current; 5819 $finalConstVarOrType?$ id = n0.current;
5738 $s$ 5820 $s$
5739 \} 5821 \}
5740 \end{dartCode} 5822 \end{dartCode}
5741 where \code{n0} is an identifier that does not occur anywhere in the program, ex cept that for purposes of static typechecking, it is checked under the assumptio n that $n0$ is declared to be of type $T$, where $T$ is the static type of $e.it erator$. 5823 where \code{n0} is an identifier that does not occur anywhere in the program, ex cept that for purposes of static typechecking, it is checked under the assumptio n that $n0$ is declared to be of type $T$, where $T$ is the static type of $e.it erator$.
5742 5824
5743
5744
5745 \subsubsection{Asynchronous For-in} 5825 \subsubsection{Asynchronous For-in}
5746 \LMLabel{asynchronousFor-in} 5826 \LMLabel{asynchronousFor-in}
5747 5827
5748 \LMHash{} 5828 \LMHash{}
5749 A for-in statement may be asynchronous. The asynchronous form is designed to ite rate over streams. An asynchronous for loop is distinguished by the keyword \AWA IT{} immediately preceding the keyword \FOR. 5829 A for-in statement may be asynchronous. The asynchronous form is designed to ite rate over streams. An asynchronous for loop is distinguished by the keyword \AWA IT{} immediately preceding the keyword \FOR.
5750 5830
5751 \LMHash{} 5831 \LMHash{}
5752 Execution of a for-in statement of the form \code{\AWAIT{} \FOR{} (finalConstVa rOrType? id \IN{} $e$) $s$} proceeds as follows: 5832 Execution of a for-in statement, $f$, of the form \code{\AWAIT{} \FOR{} (finalCo nstVarOrType? id \IN{} $e$) $s$} proceeds as follows:
5753 5833
5754 \LMHash{} 5834 \LMHash{}
5755 The expression $e$ is evaluated to an object $o$. It is a dynamic error if $o$ i s not an instance of a class that implements \code{Stream}. Otherwise, the expre ssion \code{\AWAIT{} $v_f$} (\ref{awaitExpressions}) is evaluated, where $v_f$ is a fresh variable whose value is a fresh instance (\ref{generativeConstructors }) $f$ implementing the built-in class \code{Future}. 5835 The expression $e$ is evaluated to an object $o$.
5836 It is a dynamic error if $o$ is not an instance of a class that implements \code {Stream}.
5756 5837
5757 \LMHash{} 5838 \LMHash{}
5758 The stream $o$ is listened to, and on each data event in $o$ the statement $s$ is executed with \code{id} bound to the value of the current element of the stre am. If $s$ raises an exception, or if $o$ raises an exception, then $f$ is compl eted with that exception. Otherwise, when all events in the stream $o$ have been processed, $f$ is completed with \NULL{} (\ref{null}). 5839 The stream associated with the innermost enclosing asynchronous for loop, if any , is paused.
5840 The stream $o$ is listened to, producing a stream subscription $u$,
5841 and execution of the asynchronous for-in loop is suspended
5842 until a stream event is available.
5843 This allows other asynchronous events to execute while this loop is waiting for stream events.
5844
5845 Pausing an asynchronous for loop means pausing the associated stream subscriptio n.
5846 A stream subscription is paused by calling its \code{pause} method.
5847 \commentary{
5848 The \code{pause} call can throw, although that should never happen for a correct ly implemented stream.
5849 }
5850 If the subscription is already paused, the \code{pause} call may be omitted.
5759 5851
5760 \LMHash{} 5852 \LMHash{}
5761 Let $u$ be the stream associated with the immediately enclosing asynchronous for loop or generator function (\ref{functions}), if any. If another event $e_u$ of $u$ occurs before execution of $s$ is complete, handling of $e_u$ must wait unt il $s$ is complete. 5853 For each {\em data event} from $u$,
5854 the statement $s$ is executed with \code{id} bound to the value of the current d ata event.
5762 5855
5763 \rationale{ 5856 \LMHash{}
5764 The future $f$ and the corresponding \AWAIT{} expression ensure that execution s uspends as an asynchronous for loop begins and resumes after the \FOR{} statemen t when it ends. They also ensure that the stream of any enclosing asynchronous \ FOR{} loop is paused for the duration of this loop. 5857 If another event $e_u$ of $u$ occurs before execution of $s$ is complete, handli ng of $e_u$ must wait until $s$ is complete.
5858
5859 \LMHash{}
5860 If execution of $s$ {\em continues} with no label, or with a label that prefixes the asynchronous for statement (\ref{labels}), then the execution of $s$ is tre ated as if it had completed normally.
5861
5862 If execution of $s$ does not completing normally, the subscription $u$ is cancel ed by evaluating \code{\AWAIT{} v.cancel()} where $v$ is a fresh variable refere ncing the stream subscription $u$.
5863 If that evaluation throws,
5864 execution of $f$ throws the same exception and stack trace.
5865 Otherwise execution of $f$ completes in the same way as the execution of $s$.
5866 % Notice: The previous specification was unclear about what happened when
5867 % a subscripton is canceled. This text is explicit, and existing
5868 % implementations may not properly await the cancel call.
5869
5870 Otherwise the execution of $f$ is suspended again, waiting for the next stream s ubscription event, and $u$ is resumed if it has been paused.
5871 If $u$ has been paused more than once, the \code{resume} method is called
5872 until $u$ is no longer paused.
5873 \commentary{
5874 The \code{resume} call can throw, in which case the asynchronous for
5875 loop also throws. That should never happen for a correctly implemented stream.
5765 } 5876 }
5766 5877
5767 \LMHash{} 5878 \LMHash{}
5879 On the first {\em error event} from $u$,
eernst 2016/10/19 13:37:05 We shouldn't specify 'first': Saying that somethin
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Error events are a defined term for streams. They
5880 with error object $e$ and stack trace $st$,
5881 the subscription $u$ is canceled by evaluating \code{\AWAIT{} v.cancel()}
5882 where $v$ is a fresh variable referencing the stream subscription $u$.
5883 If that evaluation throws,
5884 execution of $f$ throws the same exception object and stack trace.
5885 Otherwise execution of $f$ throws with $e$ as exception object and $st$ as stack trace.
5886
5887 \LMHash{}
5888 When $u$ is done, execution of $f$ completes normally.
5889
5890 \LMHash{}
5768 It is a compile-time error if an asynchronous for-in statement appears inside a synchronous function (\ref{functions}). It is a compile-time error if a traditio nal for loop (\ref{forLoop}) is prefixed by the \AWAIT{} keyword. 5891 It is a compile-time error if an asynchronous for-in statement appears inside a synchronous function (\ref{functions}). It is a compile-time error if a traditio nal for loop (\ref{forLoop}) is prefixed by the \AWAIT{} keyword.
5769 5892
5770 \rationale{An asynchronous loop would make no sense within a synchronous functio n, for the same reasons that an await expression makes no sense in a synchronous function.} 5893 \rationale{An asynchronous loop would make no sense within a synchronous functio n, for the same reasons that an await expression makes no sense in a synchronous function.}
5771 5894
5772 5895
5773 \subsection{While} 5896 \subsection{While}
5774 \LMLabel{while} 5897 \LMLabel{while}
5775 5898
5776 \LMHash{} 5899 \LMHash{}
5777 The while statement supports conditional iteration, where the condition is evalu ated prior to the loop. 5900 The while statement supports conditional iteration, where the condition is evalu ated prior to the loop.
5778 5901
5779 \begin{grammar} 5902 \begin{grammar}
5780 {\bf whileStatement:} 5903 {\bf whileStatement:}
5781 \WHILE{} `(' expression `)' statement % could do top level here, and in f or 5904 \WHILE{} `(' expression `)' statement % could do top level here, and in f or
5782 . 5905 .
5783 \end{grammar} 5906 \end{grammar}
5784 5907
5785 \LMHash{} 5908 \LMHash{}
5786 Execution of a while statement of the form \code{\WHILE{} ($e$) $s$;} proceeds as follows: 5909 Execution of a while statement of the form \code{\WHILE{} ($e$) $s$;} proceeds a s follows:
5787 5910
5788 \LMHash{} 5911 \LMHash{}
5789 The expression $e$ is evaluated to an object $o$. Then, $o$ is subjected to boo lean conversion (\ref{booleanConversion}), producing an object $r$. If $r$ is \ TRUE{}, then the statement $\{s\}$ is executed and then the while statement is r e-executed recursively. If $r$ is \FALSE{}, execution of the while statement is complete. 5912 The expression $e$ is evaluated to an object $o$. Then, $o$ is subjected to bool ean conversion (\ref{booleanConversion}), producing an object $r$.
5913
eernst 2016/10/19 13:37:07 Please insert \LMHash{} or delete the empty line.
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Why? Do we need \LMHash between all paragraphs?
5914 If $r$ is \TRUE{}, then the statement $\{s\}$ is executed.
5915 If that execution {\em completes normally} or it {\em continues} with no label o r with a label that prefixes the \WHILE{} statement (\ref{labels}), then the whi le statement is re-executed.
eernst 2016/10/19 13:37:06 Should cover the situation where $s$ completes wit
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
5916
5917 If $r$ is \FALSE{}, then execution of the while statement completes normally.
5790 5918
5791 \LMHash{} 5919 \LMHash{}
5792 It is a static type warning if the static type of $e$ may not be assigned to \co de{bool}. 5920 It is a static type warning if the static type of $e$ may not be assigned to \co de{bool}.
5793 5921
5794 5922
5795 \subsection{Do} 5923 \subsection{Do}
5796 \LMLabel{do} 5924 \LMLabel{do}
5797 5925
5798 \LMHash{} 5926 \LMHash{}
5799 The do statement supports conditional iteration, where the condition is evaluate d after the loop. 5927 The do statement supports conditional iteration, where the condition is evaluate d after the loop.
5800 5928
5801 \begin{grammar} 5929 \begin{grammar}
5802 {\bf doStatement:} 5930 {\bf doStatement:}
5803 \DO{} statement \WHILE{} `(' expression `)' `{\escapegrammar ;}'% could do t op level here 5931 \DO{} statement \WHILE{} `(' expression `)' `{\escapegrammar ;}'% could do t op level here
5804 . 5932 .
5805 \end{grammar} 5933 \end{grammar}
5806 5934
5807 5935
5808 \LMHash{} 5936 \LMHash{}
5809 Execution of a do statement of the form \code{\DO{} $s$ \WHILE{} ($e$);} proceed s as follows: 5937 Execution of a do statement of the form \code{\DO{} $s$ \WHILE{} ($e$);} proceed s as follows:
5810 5938
5811 \LMHash{} 5939 \LMHash{}
5812 The statement $\{s\}$ is executed. Then, the expression $e$ is evaluated to an o bject $o$. Then, $o$ is subjected to boolean conversion (\ref{booleanConversion }), producing an object $r$. If $r$ is \FALSE{}, execution of the do statement i s complete. If $r$ is \TRUE{}, then the do statement is re-executed recursively. 5940 The statement $\{s\}$ is executed.
5941 If that execution {\em continues} with no label, or with a label that prefixes t he do statement (\ref{labels}), then the execution of $s$ is treated as if it ha d completed normally.
5942
5943 \LMHash{}
5944 Then, the expression $e$ is evaluated to an object $o$. Then, $o$ is subjected t o boolean conversion (\ref{booleanConversion}), producing an object $r$. If $r$ is \FALSE{}, execution of the do statement completes normally.
5945 If $r$ is \TRUE{}, then the do statement is re-executed.
5813 5946
5814 \LMHash{} 5947 \LMHash{}
5815 It is a static type warning if the static type of $e$ may not be assigned to \co de{bool}. 5948 It is a static type warning if the static type of $e$ may not be assigned to \co de{bool}.
5816 5949
5817 \subsection{Switch} 5950 \subsection{Switch}
5818 \LMLabel{switch} 5951 \LMLabel{switch}
5819 5952
5820 \LMHash{} 5953 \LMHash{}
5821 The {\em switch statement} supports dispatching control among a large number of cases. 5954 The {\em switch statement} supports dispatching control among a large number of cases.
5822 5955
5823 \begin{grammar} 5956 \begin{grammar}
5824 {\bf switchStatement:} 5957 {\bf switchStatement:}
5825 \SWITCH{} `(' expression `)' `\{' switchCase* defaultCase? `\}'% could do top level here and in cases 5958 \SWITCH{} `(' expression `)' `\{' switchCase* defaultCase? `\}'% could do top level here and in cases
5826 . 5959 .
5827 5960
5828 5961
5829 {\bf switchCase:} 5962 {\bf switchCase:}
5830 label* \CASE{} expression `{\escapegrammar :}' statements 5963 label* \CASE{} expression `{\escapegrammar :}' statements
5831 . 5964 .
5832 5965
5833 {\bf defaultCase:} 5966 {\bf defaultCase:}
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
5897 \SWITCH{} ($e$) \{ 6030 \SWITCH{} ($e$) \{
5898 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$ 6031 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$
5899 $\ldots$ 6032 $\ldots$
5900 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$ 6033 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$
5901 \} 6034 \}
5902 \end{dartCode} 6035 \end{dartCode}
5903 6036
5904 proceeds as follows: 6037 proceeds as follows:
5905 6038
5906 \LMHash{} 6039 \LMHash{}
5907 The statement \code{\VAR{} id = $e$;} is evaluated, where \code{id} is a variabl e whose name is distinct from any other variable in the program. In checked mode , it is a run time error if the value of $e$ is not an instance of the same clas s as the constants $e_1 \ldots e_n$. 6040 The statement \code{\VAR{} id = $e$;} is evaluated, where \code{id} is a variabl e whose name is distinct from any other variable in the program. In checked mode , it is a run time error if the value of $e$ is not an instance of the same clas s as the constants $e_1 \ldots e_n$.
eernst 2016/10/19 13:37:05 `id` --> `$id$` in the statement, `\code{id}` -->
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
5908 6041
5909 \commentary{Note that if there are no case clauses ($n = 0$), the type of $e$ do es not matter.} 6042 \commentary{Note that if there are no case clauses ($n = 0$), the type of $e$ do es not matter.}
5910 6043
5911 \LMHash{} 6044 \LMHash{}
5912 Next, the case clause \CASE{} $e_{1}: s_{1}$ is executed if it exists. If \CASE{ } $e_{1}: s_{1}$ does not exist, then if there is a \DEFAULT{} clause it is exe cuted by executing $s_{n+1}$. 6045 Next, the case clause \CASE{} $e_{1}: s_{1}$ is matched against {\code id} if su ch a case clause exists. If \CASE{} $e_{1}: s_{1}$ does not exist, then if there is a \DEFAULT{} clause, its statements, if any, are executed (\ref{case-execute }).
eernst 2016/10/19 13:37:07 We did introduce $s_{n+1}$ a few lines earlier, so
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
5913 6046
5914 \LMHash{} 6047 \LMHash{}
5915 A case clause introduces a new scope, nested in the lexically surrounding scope. The scope of a case clause ends immediately after the case clause's statement l ist. 6048 Matching of a \CASE{} clause \CASE{} $e_{k}: s_{k}$ of a switch statement
eernst 2016/10/19 13:37:06 Why did the scope sentence go away? Surely we don'
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Because I now add braces around the statements bef
5916
5917 \LMHash{}
5918 Execution of a \CASE{} clause \CASE{} $e_{k}: s_{k}$ of a switch statement
5919 6049
5920 \begin{dartCode} 6050 \begin{dartCode}
5921 \SWITCH{} ($e$) \{ 6051 \SWITCH{} ($e$) \{
5922 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$ 6052 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$
5923 $\ldots$ 6053 $\ldots$
5924 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$ 6054 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$
5925 $label_{(n+1)1} \ldots label_{(n+1)j_{n+1}}$ \DEFAULT{}: $s_{n+1}$ 6055 $label_{(n+1)1} \ldots label_{(n+1)j_{n+1}}$ \DEFAULT{}: $s_{n+1}$
5926 \} 6056 \}
5927 \end{dartCode} 6057 \end{dartCode}
5928 6058
5929 proceeds as follows: 6059 against a value {\code id} proceeds as follows:
eernst 2016/10/19 13:37:07 `{\code id}` --> `$id$`. I actually think the fol
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
5930 6060
5931 \LMHash{} 6061 \LMHash{}
5932 The expression \code{$e_k$ == id} is evaluated to an object $o$ which is then su bjected to boolean conversion yielding a value $v$. 6062 The expression \code{$e_k$ == id} is evaluated to an object $o$ which is then su bjected to boolean conversion producing a value $v$.
eernst 2016/10/19 13:37:06 'id' --> '$id$'. In this CL we had things like 'A
Lasse Reichstein Nielsen 2016/10/31 16:54:44 I prefer not to use "yield" since that means somet
5933 If $v$ is not \TRUE{} the following case, \CASE{} $e_{k+1}: s_{k+1}$ is execut ed if it exists. If \CASE{} $e_{k+1}: s_{k+1}$ does not exist, then the \DEFAUL T{} clause is executed by executing $s_{n+1}$. 6063 If $v$ is not \TRUE{} the following case, \CASE{} $e_{k+1}: s_{k+1}$ is matched against {\code id} if such a case clause exists. If \CASE{} $e_{k+1}: s_{k+1}$ d oes not exist, then the \DEFAULT{} clause's statements are executed (\ref{case-e xecute}).
eernst 2016/10/19 13:37:07 'If $v$ is not \TRUE{}, $id$ is matched against th
Lasse Reichstein Nielsen 2016/10/31 16:54:44 \ref{case-execute} is about executing the *stateme
5934 If $v$ is \TRUE{}, let $h$ be the smallest number such that $h \ge k$ and $s_h $ is non-empty. If no such $h$ exists, let $h = n + 1$. The sequence of stateme nts $s_h$ is then executed. 6064 If $v$ is \TRUE{}, let $h$ be the smallest number such that $h \ge k$ and $s_h$ is non-empty. If no such $h$ exists, let $h = n + 1$. The statements $s_h$ are t hen executed (\ref{case-execute}).
5935 If execution reaches the point after $s_h$ then a runtime error occurs, unless $h = n+1$.
5936 6065
5937 \LMHash{} 6066 \LMHash{}
5938 Execution of a \CASE{} clause \CASE{} $e_{k}: s_{k}$ of a switch statement 6067 Matching of a \CASE{} clause \CASE{} $e_{k}: s_{k}$ of a switch statement
5939 6068
5940 \begin{dartCode} 6069 \begin{dartCode}
5941 \SWITCH{} ($e$) \{ 6070 \SWITCH{} ($e$) \{
5942 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$ 6071 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$
5943 $\ldots$ 6072 $\ldots$
5944 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$ 6073 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$
5945 \} 6074 \}
5946 \end{dartCode} 6075 \end{dartCode}
5947 6076
5948 proceeds as follows: 6077 against the value of a variable {\code id} proceeds as follows:
5949 6078
5950 \LMHash{} 6079 \LMHash{}
5951 The expression \code{$e_k$ == id} is evaluated to an object $o$ which is then su bjected to boolean conversion yielding a value $v$. 6080 The expression \code{$e_k$ == id} is evaluated to an object $o$ which is then su bjected to boolean conversion producing a value $v$.
eernst 2016/10/19 13:37:07 Keep `yielding`.
Lasse Reichstein Nielsen 2016/10/31 16:54:44 evalautes to
5952 If $v$ is not \TRUE{} the following case, \CASE{} $e_{k+1}: s_{k+1}$ is execut ed if it exists. 6081 If $v$ is not \code{true} the following case, \CASE{} $e_{k+1}: s_{k+1}$ is matc hed against {\code id} if it exists.
eernst 2016/10/19 13:37:07 We have no existing occurrences of `\code{true}`,
Lasse Reichstein Nielsen 2016/10/31 16:54:45 I see varying uses of true and \TRUE{}, but \code{
5953 If $v$ is \TRUE{}, let $h$ be the smallest integer such that $h \ge k$ and $s_ h$ is non-empty. The sequence of statements $s_h$ is executed if it exists. 6082 If $v$ is \code{true}, let $h$ be the smallest integer such that $h \ge k$ and $ s_h$ is non-empty. If no such $h$ exists, let $h$ be $n$. The sequence of statem ents $s_h$ is executed (\ref{case-execute}).
eernst 2016/10/19 13:37:06 '\code{true}' --> 'true', drop the \ref.
Lasse Reichstein Nielsen 2016/10/31 16:54:43 \TRUE{}, keeping the ref with a slight rewording.
5954 If execution reaches the point after $s_h$ then a runtime error occurs, unless $h = n$.
5955 6083
5956 6084
5957 \commentary{
5958 In other words, there is no implicit fall-through between non-empty cases. The l ast case in a switch (default or otherwise) can `fall-through' to the end of the statement.
5959 }
5960
5961 \LMHash{} 6085 \LMHash{}
5962 It is a static warning if the type of $e$ may not be assigned to the type of $e_ k$. It is a static warning if the last statement of the statement sequence $s_k$ is not a \BREAK{}, \CONTINUE{}, \RETURN{} or \THROW{} statement. 6086 It is a static warning if the type of $e$ may not be assigned to the type of $e_ k$. It is a static warning if the last statement of the statement sequence $s_k$ is not a \BREAK{}, \CONTINUE{}, \RETURN{} or \THROW{} statement.
5963 6087
5964 \rationale{ 6088 \rationale{
5965 The behavior of switch cases intentionally differs from the C tradition. Implic it fall through is a known cause of programming errors and therefore disallowed. Why not simply break the flow implicitly at the end of every case, rather than requiring explicit code to do so? This would indeed be cleaner. It would also be cleaner to insist that each case have a single (possibly compound) statement . We have chosen not to do so in order to facilitate porting of switch statemen ts from other languages. Implicitly breaking the control flow at the end of a c ase would silently alter the meaning of ported code that relied on fall-through, potentially forcing the programmer to deal with subtle bugs. Our design ensures that the difference is immediately brought to the coder's attention. The progr ammer will be notified at compile-time if they forget to end a case with a state ment that terminates the straight-line control flow. We could make this warning a compile-time error, but refrain from doing so because do not wish to force the programmer to deal with this issue immediately while porting code. If develope rs ignore the warning and run their code, a run time error will prevent the prog ram from misbehaving in hard-to-debug ways (at least with respect to this issue) . 6089 The behavior of switch cases intentionally differs from the C tradition. Implic it fall through is a known cause of programming errors and therefore disallowed. Why not simply break the flow implicitly at the end of every case, rather than requiring explicit code to do so? This would indeed be cleaner. It would also be cleaner to insist that each case have a single (possibly compound) statement . We have chosen not to do so in order to facilitate porting of switch statemen ts from other languages. Implicitly breaking the control flow at the end of a c ase would silently alter the meaning of ported code that relied on fall-through, potentially forcing the programmer to deal with subtle bugs. Our design ensures that the difference is immediately brought to the coder's attention. The progr ammer will be notified at compile-time if they forget to end a case with a state ment that terminates the straight-line control flow. We could make this warning a compile-time error, but refrain from doing so because do not wish to force the programmer to deal with this issue immediately while porting code. If develope rs ignore the warning and run their code, a run time error will prevent the prog ram from misbehaving in hard-to-debug ways (at least with respect to this issue) .
5966 6090
5967 The sophistication of the analysis of fall-through is another issue. For now, we have opted for a very straightforward syntactic requirement. There are obviousl y situations where code does not fall through, and yet does not conform to these simple rules, e.g.: 6091 The sophistication of the analysis of fall-through is another issue. For now, we have opted for a very straightforward syntactic requirement. There are obviousl y situations where code does not fall through, and yet does not conform to these simple rules, e.g.:
5968 } 6092 }
5969 6093
5970 \begin{dartCode} 6094 \begin{dartCode}
(...skipping 11 matching lines...) Expand all
5982 \begin{itemize} 6106 \begin{itemize}
5983 \item The switch statement does not have a default clause. 6107 \item The switch statement does not have a default clause.
5984 \item The static type of $e$ is an enumerated typed with elements $id_1, \ldots , id_n$. 6108 \item The static type of $e$ is an enumerated typed with elements $id_1, \ldots , id_n$.
5985 \item The sets $\{e_1, \ldots, e_k\} $ and $\{id_1, \ldots, id_n\}$ are not the same. 6109 \item The sets $\{e_1, \ldots, e_k\} $ and $\{id_1, \ldots, id_n\}$ are not the same.
5986 \end{itemize} 6110 \end{itemize}
5987 6111
5988 \commentary{ 6112 \commentary{
5989 In other words, a warning will be issued if a switch statement over an enum is n ot exhaustive. 6113 In other words, a warning will be issued if a switch statement over an enum is n ot exhaustive.
5990 } 6114 }
5991 6115
6116 \LMHash{}
6117 \subsubsection{ Switch case statements}
eernst 2016/10/19 13:37:06 Argh, we don't have to have that weird space ('{ S
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Thanks. :)
6118 \LMLabel{case-execute}
6119 Execution of the statements $s_h$ of a switch statement
eernst 2016/10/19 13:37:05 Please add empty line and `\LMHash{}` in front of
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6120
6121 \begin{dartCode}
6122 \SWITCH{} ($e$) \{
6123 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$
6124 $\ldots$
6125 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$
6126 \}
6127 \end{dartCode}
6128
6129 or a switch statement
6130
6131 \begin{dartCode}
6132 \SWITCH{} ($e$) \{
6133 $label_{11} \ldots label_{1j_1}$ \CASE{} $e_1: s_1$
6134 $\ldots$
6135 $label_{n1} \ldots label_{nj_n}$ \CASE{} $e_n: s_n$
6136 $label_{(n+1)1} \ldots label_{(n+1)j_{n+1}}$ \DEFAULT{}: $s_{n+1}$
6137 \}
6138 \end{dartCode}
6139
6140 proceeds as follows:
6141
6142 \LMHash{}
6143 Execute $\{s_h\}$.
6144 If this execution completes normally,
6145 and if $s_h$ is not the statements of the last case of the switch
6146 ($h = n$ if there is no \DEFAULT{} clause,
6147 $h = n+1$ if there is a \DEFAULT{} clause),
6148 then the execution of the switch case throws an error.
6149 Otherwise $s_h$ are the last statements of the switch case,
6150 and execution of the switch case completes normally.
6151
6152 \commentary{
6153 In other words, there is no implicit fall-through between non-empty cases. The l ast case in a switch (default or otherwise) can `fall-through' to the end of the statement.
6154 }
6155
6156 If execution of $\{s_h\}$ {\em breaks} with no label, or with a label that prefi xes the switch statement, then the execution of the switch statement completes n ormally.
6157
6158 If execution of $\{s_h\}$ {\em continues} with a label, and the label is $label_ {ij}$, where $1 \le i \le n+1$ if the \SWITCH{} statement has a \DEFAULT{}, or $ 1 \le i \le n$ if there is no \DEFAULT{}, and where $1 \le j \le j_{i}$, then
6159 execution of the switch statement continues with the case labeled by that label.
eernst 2016/10/19 13:37:06 This again shows that 'execution continues' is too
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Removed entire line, it's purely explanatory, the
6160 let $h$ be the smallest number such that $h \ge i$ and $s_h$ is non-empty. If no such $h$ exists, and let $h = n + 1$ if the \SWITCH{} statement has a \DEFAULT{ }, otherwise let $h = n$.
6161 The statements $s_h$ are then executed (\ref{case-execute}).
6162
6163 If execution of $\{s_h\}$ completes in any other way, execution of the \SWITCH{} statement completes in the same way.
6164
eernst 2016/10/19 13:37:06 It's hard to tell which is better, but we should c
Lasse Reichstein Nielsen 2016/10/31 16:54:44 This extra text and index manipulation is only nec
5992 6165
5993 \subsection{ Rethrow} 6166 \subsection{ Rethrow}
5994 \LMLabel{rethrow} 6167 \LMLabel{rethrow}
5995 6168
5996 6169
5997 \LMHash{} 6170 \LMHash{}
5998 The {\em rethrow statement} is used to re-raise an exception. 6171 The {\em rethrow statement} is used to re-throw an exception and its associated stack trace.
5999 6172
6000 \begin{grammar} 6173 \begin{grammar}
6001 {\bf rethrowStatement:} 6174 {\bf rethrowStatement:}
6002 \RETHROW{} `{\escapegrammar ;}' 6175 \RETHROW{} `{\escapegrammar ;}'
6003 . 6176 .
6004 \end{grammar} 6177 \end{grammar}
6005 6178
6006 \LMHash{} 6179 \LMHash{}
6007 Execution of a \code{\RETHROW{}} statement proceeds as follows: 6180 Execution of a \code{\RETHROW{}} statement proceeds as follows:
6008 6181
6009 \LMHash{} 6182 \LMHash{}
6010 Let $f$ be the immediately enclosing function, and let \code{\ON{} $T$ \CATCH{} ($p_1$, $p_2$)} be the immediately enclosing catch clause (\ref{try}). 6183 Let $f$ be the immediately enclosing function, and let \code{\ON{} $T$ \CATCH{} ($p_1$, $p_2$)} be the immediately enclosing catch clause (\ref{try}).
6011 6184
6012 \rationale{ 6185 \rationale{
6013 A \RETHROW{} statement always appears inside a \CATCH{} clause, and any \CATCH{} clause is semantically equivalent to some \CATCH{} clause of the form \code{\ON {} $T$ \CATCH{} (p1, p2)}. So we can assume that the \RETHROW{} is enclosed in a \CATCH{} clause of that form. 6186 A \RETHROW{} statement always appears inside a \CATCH{} clause, and any \CATCH{} clause is semantically equivalent to some \CATCH{} clause of the form \code{\ON {} $T$ \CATCH{} (p1, p2)}. So we can assume that the \RETHROW{} is enclosed in a \CATCH{} clause of that form.
6014 } 6187 }
6015 6188
6016 \LMHash{} 6189 \LMHash{}
6017 The current exception (\ref{throw}) is set to $p_1$, the current return value (\ ref{return}) becomes undefined, and the active stack trace (\ref{try}) is set to $p_2$. 6190 The \RETHROW{} statement {\em throws} (\ref{completion}) with $p_1$ as the excep tion object and $p_2$ as the stack trace.
6018
6019 \LMHash{}
6020 If $f$ is marked \ASYNC{} or \ASYNC* (\ref{functions}) and there is a dynamicall y enclosing exception handler (\ref{try}) $h$ introduced by the current activati on, control is transferred to $h$, otherwise $f$ terminates.
6021
6022 \rationale{
6023 In the case of an asynchronous function, the dynamically enclosing exception han dler is only relevant within the function. If an exception is not caught within the function, the exception value is channelled through a future or stream rathe r than propagating via exception handlers.
6024 }
6025
6026 \LMHash{}
6027 Otherwise, control is transferred to the innermost enclosing exception handler.
6028
6029 \commentary{The change in control may result in multiple functions terminating i f these functions do not catch the exception via a \CATCH{} or \FINALLY{} clause , both of which introduce a dynamically enclosing exception handler.}
6030 6191
6031 \LMHash{} 6192 \LMHash{}
6032 It is a compile-time error if a \code{\RETHROW{}} statement is not enclosed wit hin an \ON-\CATCH{} clause. 6193 It is a compile-time error if a \code{\RETHROW{}} statement is not enclosed wit hin an \ON-\CATCH{} clause.
6033 6194
6034 6195
6035
6036 \subsection{ Try} 6196 \subsection{ Try}
6037 \LMLabel{try} 6197 \LMLabel{try}
6038 6198
6039 \LMHash{} 6199 \LMHash{}
6040 The try statement supports the definition of exception handling code in a struct ured way. 6200 The try statement supports the definition of exception handling code in a struct ured way.
6041 6201
6042 \begin{grammar} 6202 \begin{grammar}
6043 {\bf tryStatement:} 6203 {\bf tryStatement:}
6044 \TRY{} block (onPart+ finallyPart? $|$ finallyPart) 6204 \TRY{} block (onPart+ finallyPart? $|$ finallyPart)
6045 . 6205 .
6046 6206
6047 {\bf onPart:}catchPart block; 6207 {\bf onPart:}catchPart block;
6048 \ON{} type catchPart? block 6208 \ON{} type catchPart? block
6049 . 6209 .
6050 6210
6051 {\bf catchPart:} 6211 {\bf catchPart:}
6052 \CATCH{} `(' identifier (`,' identifier)? `)' 6212 \CATCH{} `(' identifier (`,' identifier)? `)'
6053 . 6213 .
6054 6214
6055 {\bf finallyPart:} 6215 {\bf finallyPart:}
6056 \FINALLY{} block 6216 \FINALLY{} block
6057 . 6217 .
6058 \end{grammar} 6218 \end{grammar}
6059 6219
6060 \LMHash{} 6220 \LMHash{}
6061 A try statement consists of a block statement, followed by at least one of: 6221 A try statement consists of a block statement, followed by at least one of:
6062 \begin{enumerate} 6222 \begin{enumerate}
6063 \item 6223 \item
6064 A set of \ON{}-\CATCH{} clauses, each of which specifies (either explicitly or implicitly) the type of exception object to be handled, one or two exception par ameters and a block statement. 6224 A set of \ON{}-\CATCH{} clauses, each of which specifies (either explicitly or i mplicitly) the type of exception object to be handled, two exception parameters and a block statement.
eernst 2016/10/19 13:37:06 We still need to textually allow the omission of t
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Yes, and the grammar above does. There is "catch w
6065 \item 6225 \item
6066 A \FINALLY{} clause, which consists of a block statement. 6226 A \FINALLY{} clause, which consists of a block statement.
6067 \end{enumerate} 6227 \end{enumerate}
6068 6228
6069 \rationale{ 6229 \rationale{
6070 The syntax is designed to be upward compatible with existing Javascript programs . The \ON{} clause can be omitted, leaving what looks like a Javascript catch cl ause. 6230 The syntax is designed to be upward compatible with existing Javascript programs . The \ON{} clause can be omitted, leaving what looks like a Javascript catch cl ause.
6071 } 6231 }
6072 6232
6073 \LMHash{} 6233 \LMHash{}
6074 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$ } {\em matches} an object $o$ if the type of $o$ is a subtype of $T$. If $T$ is a malformed or deferred type (\ref{staticTypes}), then performing a match ca uses a run time error. 6234 A try statement of the form \code{\TRY{} $s_1$ $on-catch_1 \ldots on-catch_n$;} is equivalent to the statement \code{\TRY{} $s_1$ $on-catch_1 \ldots on-catch_n$ \FINALLY{} $\{\}$}.
6075 6235
6076 \commentary { 6236 \LMHash{}
6077 It is of course a static warning if $T$ is a deferred or malformed type. 6237 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ \CATCH{} ($p_1$) $s$} is e quivalent to an \ON{}-\CATCH{} clause \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$ } where $p_2$ is an identifier that does not occur anywhere else in the program.
eernst 2016/10/19 13:37:07 We have 12 occurrences of `fresh variable` and onl
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Agree
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Agree
6078 } 6238
6239 \LMHash{}
6240 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ $s$} is equivalent to an \ ON{}-\CATCH{} clause \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$} where $p_1$ and $p_2$ are identifiers that do not occur anywhere else in the program.
eernst 2016/10/19 13:37:06 `fresh variable`
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6241
6242 \LMHash{}
6243 An \ON{}-\CATCH{} clause of the form \code{\CATCH{} ($p$) $s$} is equivalent to an \ON{}-\CATCH{} clause \code{\ON{} \DYNAMIC{} \CATCH{} ($p$, $p_2$) $s$} wher e $p_2$ is an identifier that does not occur anywhere else in the program.
eernst 2016/10/19 13:37:06 `fresh variable`
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
6244
6245 An \ON{}-\CATCH{} clause of the form \code{\CATCH{} ($p_1, p_2$) $s$} is equival ent to an \ON{}-\CATCH{} clause \code{\ON{} \DYNAMIC{} \CATCH{} ($p_1, p_2$) $s$ }.
6079 6246
6080 \LMHash{} 6247 \LMHash{}
6081 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$ } introduces a new scope $CS$ in which final local variables specified by $p_1$ and $p_2$ are defined. The statement $s$ is enclosed within $CS$. The static typ e of $p_1$ is $T$ and the static type of $p_2$ is \code{StackTrace}. 6248 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$ } introduces a new scope $CS$ in which final local variables specified by $p_1$ and $p_2$ are defined. The statement $s$ is enclosed within $CS$. The static typ e of $p_1$ is $T$ and the static type of $p_2$ is \code{StackTrace}.
6082 6249
6083 6250
6084 \LMHash{} 6251 \LMHash{}
6085 An \ON{}-\CATCH{} clause of the form \code{\ON{} $T$ \CATCH{} ($p_1$) $s$} is e quivalent to an \ON{}-\CATCH{} clause \code{\ON{} $T$ \CATCH{} ($p_1, p_2$) $s$ } where $p_2$ is an identifier that does not occur anywhere else in the program. 6252 Execution of a \TRY{} statement $s$ on the form:
6086 6253 \begin{dartCode}
6254 \TRY{} b
6255 \ON{} $T_1$ \CATCH{} ($e_1$, $t_1$) c_1
6256 \ldots{}
6257 \ON{} $T_n$ \CATCH{} ($e_n$, $t_n$) c_n
6258 \FINALLY{} f
6259 \end{dartCode}
6260 proceeds as follows:
6087 6261
6088 \LMHash{} 6262 \LMHash{}
6089 An \ON{}-\CATCH{} clause of the form \code{\CATCH{} ($p$) $s$} is equivalent to an \ON{}-\CATCH{} clause \code{\ON{} \DYNAMIC{} \CATCH{} ($p$) $s$}. An \ON{}- \CATCH{} clause of the form \code{\CATCH{} ($p_1, p_2$) $s$} is equivalent to a n \ON{}-\CATCH{} clause \code{\ON{} \DYNAMIC{} \CATCH{} ($p_1, p_2$) $s$}. 6263 First $b$ is executed.
6264 If execution of $b$ {\em throws} (\ref{completion}) with exception object $e$ an d stack trace $t$, then $e$ and $t$ are matched against the \ON{}-\CATCH{} claus es to yield a new completion (\ref{on-catch}).
eernst 2016/10/19 13:37:07 Just 'throws'.
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
6090 6265
6266 Then, even if execution of $b$ did not complete normally or matching against the \ON{}-\CATCH{} clauses did not complete normally, the $f$ block is executed.
6091 6267
6092 %If an explicit type is associated with of $p_2$, it is a static warning if that type is not \code{Object} or \DYNAMIC{}. 6268 If execution of $f$ does not complete normally,
6269 execution of the \TRY{} statement completes in the same way.
6270 Otherwise if execution of $b$ threw, the \TRY{} statement completes in the same way as the matching against the \ON{}-\CATCH{} clauses.
eernst 2016/10/19 13:37:05 `threw (\ref{completion})`
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
6271 Otherwise the \TRY{} statement completes in the same way as the execution of $b$ .
6272
6273 \subsubsection{\ON{}-\CATCH{} clauses}
eernst 2016/10/19 13:37:05 `\LMLabel{..}`, empty line.
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Why add a label if it isn't referenced?
6274 \LMHash{}
6275 Matching an exception object $e$ and stack trace $t$ against a (potentially empt y) sequence of \ON{}-\CATCH{} clauses on the form:
6276 \begin{dartCode}
6277 \ON{} $T_1$ \CATCH{} ($e_1$, $st_1$) \{ $s_1$ \}
6278 \ldots
6279 \ON{} $T_n$ \CATCH{} ($e_n$, $st_n$) \{ $s_n$ \}
6280 \end{dartCode}
eernst 2016/10/19 13:37:06 +`proceeds as follows:`, and no colon after `form`
6093 6281
6094 \LMHash{} 6282 \LMHash{}
6095 The {\em active stack trace} is an object whose \code{toString()} method produce s a string that is a record of exactly those function activations within the cur rent isolate that had not completed execution at the point where the current exc eption (\ref{throw}) was thrown. 6283 If there are no \ON{}-\CATCH{} clauses ($n = 0$), matching completes by {\em thr owing} the exception object $e$ and stack trace $t$.
eernst 2016/10/19 13:37:06 `completes by throwing (\ref{completion})`
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Throws.
6096 %\begin{enumerate}
6097 %\item Started execution after the currently executing function.
6098 %\item Had not completed execution at the point where the exception caught by th e currently executing \ON{}-\CATCH{} clause was initially thrown.
6099 %\commentary{The active stack trace contains the frames between the exception ha ndling code and the original point when an exception is thrown, not where it was rethrown.}
6100 %\end{enumerate}
6101 6284
6102 \commentary{ 6285 Otherwise the exception is matched against the first clause.
6103 This implies that no synthetic function activations may be added to the trace, n or may any source level activations be omitted. 6286 \LMHash{}
eernst 2016/10/19 13:37:05 Move `LMHash` up in front of the paragraph, and ad
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done.
6104 This means, for example, that any inlining of functions done as an optimization must not be visible in the trace. Similarly, any synthetic routines used by the implementation must not appear in the trace.
6105 6287
6106 Nothing is said about how any native function calls may be represented in the tr ace. 6288 If $T_1$ is a malformed or deferred type (\ref{staticTypes}), then performing a match causes a run time error.
6107 } 6289 \commentary {
6108 6290 It is of course a static warning if $T_i$, $1 \le i \le n$ is a deferred or malf ormed type.
eernst 2016/10/19 13:37:06 Delete 'of course' and move this sentence up a few
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6109 \commentary{
6110 Note that we say nothing about the identity of the stack trace, or what notion o f equality is defined for stack traces.
6111 }
6112
6113 % Sadly, the info below cannot be computed efficiently. It would need to be comp uted at the throw point, since at latte points it might be destroyed. Native cod e in calling frames executes relative to the stack pointer, which therefore need s to be reset as each frame is unwound. This means that the
6114 % OS kernel can dispose of this stack memory - it is not reliably preserved. And such code must execute if only to test if the exception should be caught or sen t onward.
6115
6116 % For each such function activation, the active stack trace includes the name of the function, the bindings of all its formal parameters, local variables and \T HIS{}, and the position at which the function was executing.
6117
6118 % Is this controversial? We were thinking of viewing the trace as a List<Invoca tion>,
6119 % but that won't capture the receiver or the locals. More generally, we need a standard interface that describes these traces, so one can type the stack trace variable in the catch.
6120
6121 \commentary{The term position should not be interpreted as a line number, but r ather as a precise position - the exact character index of the expression that raised the exception. }
6122
6123 % A position can be represented via a Token. If we make that part of the core r eflection facility, we can state this here.
6124
6125 \LMHash{}
6126 A try statement \TRY{} $s_1$ $on-catch_1 \ldots on-catch_n$ \FINALLY{} $s_f$ d efines an exception handler $h$ that executes as follows:
6127
6128 \LMHash{}
6129 The \ON{}-\CATCH{} clauses are examined in order, starting with $catch_1$, until either an \ON{}-\CATCH{} clause that matches the current exception (\ref{throw} ) is found, or the list of \ON{}-\CATCH{} clauses has been exhausted. If an \ON{ }-\CATCH{} clause $on-catch_k$ is found, then $p_{k1}$ is bound to the current e xception, $p_{k2}$, if declared, is bound to the active stack trace, and then $catch_k$ is executed. If no \ON{}-\CATCH{} clause is found, the \FINALLY{} clau se is executed. Then, execution resumes at the end of the try statement.
6130
6131
6132 \LMHash{}
6133 A finally clause \FINALLY{} $s$ defines an exception handler $h$ that executes a s follows:
6134
6135 \LMHash{}
6136 Let $r$ be the current return value (\ref{return}). Then the current return valu e becomes undefined. Any open streams associated with any asynchronous for loops (\ref{asynchronousFor-in}) and yield-each (\ref{yieldEach}) statements executin g within the dynamic scope of $h$ are canceled, in the order of their nesting, i nnermost first.
6137
6138 \rationale{
6139 Streams left open by for loops that were escaped for whatever reason would be ca nceled at function termination, but it is best to cancel them as soon as possibl e.
6140 } 6291 }
6141 6292
6142 \LMHash{} 6293 \LMHash{}
6143 Then the \FINALLY{} clause is executed. Let $m$ be the immediately enclosing fun ction. If $r$ is defined then the current return value is set to $r$ and then: 6294 Otherwise, if the type of $e$ is a subtype of $T_1$, then the first clause match es, and then $e_1$ is bound to the exception object $e$ and $t_1$ is bound to th e stack trace $t$, and $s_1$ is executed in this scope.
6144 \begin{itemize} 6295 The matching completes in the same way as this execution.
6145 \item
6146 if there is a dynamically enclosing error handler $g$ defined by a \FINALLY{} c lause in $m$, control is transferred to $g$.
6147 \item
6148 Otherwise $m$ terminates.
6149 \end{itemize}
6150
6151 Otherwise, execution resumes at the end of the try statement.
6152 6296
6153 \LMHash{} 6297 \LMHash{}
6154 Execution of an \ON{}-\CATCH{} clause \code{\ON{} $T$ \CATCH{} ($p_1$, $p_2$)} $ s$ of a try statement $t$ proceeds as follows: The statement $s$ is executed in the dynamic scope of the exception handler defined by the finally clause of $t$. Then, the current exception and active stack trace both become undefined. 6298 Otherwise, if the first clause did not match $e$, $e$ and $t$ are recursively ma tched against the remaining \ON{}-\CATCH{} clauses:
6155 6299 \begin{dartCode}
6156 \LMHash{} 6300 \ON{} $T_2$ \CATCH{} ($e_2$, $t_2$) \{ $s_2$ \}
6157 Execution of a \FINALLY{} clause \FINALLY{} $s$ of a try statement proceeds as f ollows: 6301 \ldots
6158 6302 \ON{} $T_n$ \CATCH{} ($e_n$, $t_n$) \{ $s_n$ \}
6159 \LMHash{} 6303 \end{dartCode}
6160 Let $x$ be the current exception and let $t$ be the active stack trace. Then the current exception and the active stack trace both become undefined. The stateme nt $s$ is executed. Then, if $x$ is defined, it is rethrown as if by a rethrow statement (\ref{rethrow}) enclosed in a \CATCH{} clause of the form \code{\CATCH {} ($v_x$, $v_t$)} where $v_x$ and $v_t$ are fresh variables bound to $x$ and $t $ respectively.
6161
6162
6163 \LMHash{}
6164 Execution of a try statement of the form \code{\TRY{} $s_1$ $on-catch_1 \ldots o n-catch_n$ \FINALLY{} $s_f$;} proceeds as follows:
6165
6166 \LMHash{}
6167 The statement $s_1$ is executed in the dynamic scope of the exception handler de fined by the try statement. Then, the \FINALLY{} clause is executed.
6168
6169 \commentary{
6170 Whether any of the \ON{}-\CATCH{} clauses is executed depends on whether a match ing exception has been raised by $s_1$ (see the specification of the throw state ment).
6171
6172 If $s_1$ has raised an exception, it will transfer control to the try statement' s handler, which will examine the catch clauses in order for a match as specifie d above. If no matches are found, the handler will execute the \FINALLY{} clause .
6173
6174 If a matching \ON{}-\CATCH{} was found, it will execute first, and then the \FIN ALLY{} clause will be executed.
6175
6176 If an exception is thrown during execution of an \ON{}-\CATCH{} clause, this wil l transfer control to the handler for the \FINALLY{} clause, causing the \FINALL Y{} clause to execute in this case as well.
6177
6178 If no exception was raised, the \FINALLY{} clause is also executed. Execution of the \FINALLY{} clause could also raise an exception, which will cause transfer of control to the next enclosing handler.
6179 }
6180
6181 \LMHash{}
6182 A try statement of the form \code{\TRY{} $s_1$ $on-catch_1 \ldots on-catch_n$;} is equivalent to the statement \code{\TRY{} $s_1$ $on-catch_1 \ldots on-catch_n$ \FINALLY{} $\{\}$}.
6183 6304
eernst 2016/10/19 13:37:06 Wow, that's a substantial improvement! Was the se
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Yes - that cancelling of the subscription of an aw
6184 6305
6185 \subsection{ Return} 6306 \subsection{ Return}
6186 \LMLabel{return} 6307 \LMLabel{return}
6187 6308
6188 \LMHash{} 6309 \LMHash{}
6189 The {\em return statement} returns a result to the caller of a synchronous funct ion, completes the future associated with an asynchronous function or terminate s the stream or iterable associated with a generator (\ref{functions}). 6310 The {\em return statement} returns a result to the caller of a synchronous funct ion, completes the future associated with an asynchronous function or terminates the stream or iterable associated with a generator (\ref{functions}).
6190 6311
6191 6312
6192 \begin{grammar} 6313 \begin{grammar}
6193 {\bf returnStatement:} 6314 {\bf returnStatement:}
6194 \RETURN{} expression? `{\escapegrammar ;}' % could do top level here 6315 \RETURN{} expression? `{\escapegrammar ;}' % could do top level here
6195 . 6316 .
6196 \end{grammar} 6317 \end{grammar}
6197 6318
6198 \commentary{
6199 Due to \FINALLY{} clauses, the precise behavior of \RETURN{} is a little more i nvolved. Whether the value a return statement is supposed to return is actually returned depends on the behavior of any \FINALLY{} clauses in effect when execut ing the return. A \FINALLY{} clause may choose to return another value, or throw an exception, or even redirect control flow leading to other returns or throws. All a return statement really does is set a value that is intended to be return ed when the function terminates.
6200 }
6201
6202 \LMHash{}
6203 The {\em current return value} is a unique value specific to a given function ac tivation. It is undefined unless explicitly set in this specification.
6204
6205 \LMHash{} 6319 \LMHash{}
6206 Executing a return statement \code{\RETURN{} $e$;} proceeds as follows: 6320 Executing a return statement \code{\RETURN{} $e$;} proceeds as follows:
6207 6321
6208 \LMHash{} 6322 \LMHash{}
6209 First the expression $e$ is evaluated, producing an object $o$. Next: 6323 First the expression $e$ is evaluated, producing an object $o$.
6210 \begin{itemize} 6324 Then the return statement completes by \em{returning} the value $o$ (\ref{comple tion}).
eernst 2016/10/19 13:37:06 The `\em{..}` construct makes a zero-argument comm
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Just: returns the value $o$
6211 \item
6212 The current return value is set to $o$ and the current exception (\ref{throw}) a nd active stack trace (\ref{try}) become undefined.
6213 \item
6214 Let $c$ be the \FINALLY{} clause of the innermost enclosing try-finally statemen t (\ref{try}), if any. If $c$ is defined, let $h$ be the handler induced by $c$. If $h$ is defined, control is transferred to $h$.
6215 \item
6216 Otherwise execution of the current method terminates.
6217 \end{itemize}
6218
6219 \commentary{
6220 In the simplest case, the immediately enclosing function is an ordinary, synchro nous non-generator, and upon function termination, the current return value is g iven to the caller. The other possibility is that the function is marked \ASYNC {}, in which case the current return value is used to complete the future associ ated with the function invocation. Both these scenarios are specified in section \ref{functionInvocation}.
6221 The enclosing function cannot be marked as generator (i.e, \ASYNC* or \SYNC*), s ince generators are not allowed to contain a statement of the form \code{\RETURN {} $e$;} as discussed below.
6222 }
6223 6325
6224 \LMHash{} 6326 \LMHash{}
6225 Let $T$ be the static type of $e$ and let $f$ be the immediately enclosing funct ion. 6327 Let $T$ be the static type of $e$ and let $f$ be the immediately enclosing funct ion.
6226 6328
6227 \LMHash{} 6329 \LMHash{}
6228 It is a static type warning if the body of $f$ is marked \ASYNC{} and the type \ code{Future$<$flatten(T)$>$} (\ref{functionExpressions}) may not be assigned to the declared return type of $f$. Otherwise, it is a static type warning if $T $ may not be assigned to the declared return type of $f$. 6330 It is a static type warning if the body of $f$ is marked \ASYNC{} and the type \ code{Future$<$flatten(T)$>$} (\ref{functionExpressions}) may not be assigned to the declared return type of $f$. Otherwise, it is a static type warning if $T $ may not be assigned to the declared return type of $f$.
6229 6331
6230 \LMHash{} 6332 \LMHash{}
eernst 2016/10/19 13:37:07 Why did the ASYNC / wrong-return-type error paragr
Lasse Reichstein Nielsen 2016/10/31 16:54:43 Good point. Must have gotten lost somewhere. I thi
6231 Let $S$ be the runtime type of $o$. In checked mode:
6232 \begin{itemize}
6233 \item If the body of $f$ is marked \ASYNC{} (\ref{functions}) it is a dynamic t ype error if $o$ is not \NULL{} (\ref{null}) and \code{Future$<$flatten(S)$>$} i s not a subtype of the actual return type (\ref{actualTypeOfADeclaration}) of $ f$.
6234 \item Otherwise, it is a dynamic type error if $o$ is not \NULL{} and the runtim e type of $o$ is not a subtype of the actual return type of $f$.
6235 \end{itemize}
6236
6237 \LMHash{}
6238 It is a compile-time error if a return statement of the form \code{\RETURN{} $e$ ;} appears in a generative constructor (\ref{generativeConstructors}). 6333 It is a compile-time error if a return statement of the form \code{\RETURN{} $e$ ;} appears in a generative constructor (\ref{generativeConstructors}).
6239 6334
6240 \rationale{ 6335 \rationale{
6241 It is quite easy to forget to add the factory prefix for a constructor, accident ally converting a factory into a generative constructor. The static checker may detect a type mismatch in some, but not all, of these cases. The rule above help s catch such errors, which can otherwise be very hard to recognize. There is no real downside to it, as returning a value from a generative constructor is meani ngless. 6336 It is quite easy to forget to add the factory prefix for a constructor, accident ally converting a factory into a generative constructor. The static checker may detect a type mismatch in some, but not all, of these cases. The rule above help s catch such errors, which can otherwise be very hard to recognize. There is no real downside to it, as returning a value from a generative constructor is meani ngless.
6242 } 6337 }
6243 6338
6244 \LMHash{} 6339 \LMHash{}
6245 It is a compile-time error if a return statement of the form \code{\RETURN{} $e$ ;} appears in a generator function. 6340 It is a compile-time error if a return statement of the form \code{\RETURN{} $e$ ;} appears in a generator function.
6246 6341
6247 \rationale{ 6342 \rationale{
6248 In the case of a generator function, the value returned by the function is the i terable or stream associated with it, and individual elements are added to that iterable using yield statements, and so returning a value makes no sense. 6343 In the case of a generator function, the value returned by the function is the i terable or stream associated with it, and individual elements are added to that iterable using yield statements, and so returning a value makes no sense.
6249 } 6344 }
6250 6345
6251 \LMHash{} 6346 \LMHash{}
6252 Let $f$ be the function immediately enclosing a return statement of the form \RE TURN{}; It is a static warning $f$ is neither a generator nor a generative cons tructor and either: 6347 Let $f$ be the function immediately enclosing a return statement of the form \RE TURN{}; It is a static warning $f$ is neither a generator nor a generative cons tructor and either:
6253 \begin{itemize} 6348 \begin{itemize}
6254 \item $f$ is synchronous and the return type of $f$ may not be assigned to \VOI D{} (\ref{typeVoid}) or, 6349 \item $f$ is synchronous and the return type of $f$ may not be assigned to \VOI D{} (\ref{typeVoid}) or,
6255 \item $f$ is asynchronous and the return type of $f$ may not be assigned to \co de{Future$<$Null$>$}. 6350 \item $f$ is asynchronous and the return type of $f$ may not be assigned to \co de{Future$<$Null$>$}.
6256 \end{itemize} 6351 \end{itemize}
6257 6352
6258 \commentary{ 6353 \commentary{
6259 Hence, a static warning will not be issued if $f$ has no declared return type, s ince the return type would be \DYNAMIC{} and \DYNAMIC{} may be assigned to \VO ID{} and to \code{Future$<$Null$>$}. However, any synchronous non-generator func tion that declares a return type must return an expression explicitly. 6354 Hence, a static warning will not be issued if $f$ has no declared return type, s ince the return type would be \DYNAMIC{} and \DYNAMIC{} may be assigned to \VO ID{} and to \code{Future$<$Null$>$}. However, any synchronous non-generator func tion that declares a return type must return an expression explicitly.
6260 } 6355 }
6261 \rationale{This helps catch situations where users forget to return a value in a return statement.} 6356 \rationale{This helps catch situations where users forget to return a value in a return statement.}
6262 6357
6263 \rationale{ An asynchronous non-generator always returns a future of some sort. If no expression is given, the future will be completed with \NULL{} and this mo tivates the requirement above.} \commentary{Leaving the return type of a functio n marked \ASYNC{} blank will be interpreted as \DYNAMIC{} as always, and cause no type error. Using \code{Future} or \code{Future$<$Object$>$} is acceptable as well, but any other type will cause a warning, since \NULL{} has no subtypes.} 6358 \rationale{ An asynchronous non-generator always returns a future of some sort. If no expression is given, the future will be completed with \NULL{} and this mo tivates the requirement above.} \commentary{Leaving the return type of a functio n marked \ASYNC{} blank will be interpreted as \DYNAMIC{} as always, and cause no type error. Using \code{Future} or \code{Future$<$Object$>$} is acceptable as well, but any other type will cause a warning, since \NULL{} has no subtypes.}
6264 6359
6265 \LMHash{} 6360 \LMHash{}
6266 A return statement with no expression, \code{\RETURN;} is executed as follows: 6361 Executing a return statement with no expression, \code{\RETURN;} {\em returns} w ith no value (\ref{completion}).
eernst 2016/10/19 13:37:07 I'd still prefer `returns with the value null (\re
Lasse Reichstein Nielsen 2016/10/31 16:54:43 This is well-defined. If a function invocation's e
6267 6362
6268 \LMHash{}
6269 If the immediately enclosing function $f$ is a generator, then:
6270 \begin{itemize}
6271 \item
6272 The current return value is set to \NULL{}.
6273 \item
6274 Let $c$ be the \FINALLY{} clause of the innermost enclosing try-finally statemen t, if any. If $c$ is defined, let $h$ be the handler induced by $c$. If $h$ is defined, control is transferred to $h$.
6275 \item
6276 Otherwise, execution of the current method terminates.
6277 \end{itemize}
6278
6279 \LMHash{}
6280 Otherwise the return statement is executed by executing the statement \code{\RE TURN{} \NULL{};} if it occurs inside a method, getter, setter or factory; otherw ise, the return statement necessarily occurs inside a generative constructor, in which case it is executed by executing \code{\RETURN{} \THIS{};}.
6281
6282 \commentary{Despite the fact that \code{\RETURN{};} is executed as if by a \code {\RETURN{} $e$;}, it is important to understand that it is not a static warning to include a statement of the form \code{\RETURN{};}
6283 %in a \VOID{} function; neither is it illegal
6284 in a generative constructor. The rules relate only to the specific syntactic for m \code{\RETURN{} $e$;}.
6285 }
6286
6287
6288 \rationale{
6289 The motivation for formulating \code{\RETURN{};} in this way stems from the basi c requirement that all function invocations indeed return a value. Function invo cations are expressions, and we cannot rely on a mandatory typechecker to always prohibit use of \VOID{} functions in expressions. Hence, a return statement mus t always return a value, even if no expression is specified.
6290
6291 The question then becomes, what value should a return statement return when no r eturn expression is given. In a generative constructor, it is obviously the obje ct being constructed (\THIS{}). A void function is not expected to participate i n an expression, which is why it is marked \VOID{} in the first place. Hence, th is situation is a mistake which should be detected as soon as possible. The stat ic rules help here, but if the code is executed, using \NULL{} leads to fast fai lure, which is desirable in this case. The same rationale applies for function b odies that do not contain a return statement at all.
6292 }
6293 6363
6294 \LMHash{} 6364 \LMHash{}
6295 It is a static warning if a function contains both one or more explicit return statements of the form \code{\RETURN;} and one or more return statements of the form \code{\RETURN{} $e$;}. 6365 It is a static warning if a function contains both one or more explicit return statements of the form \code{\RETURN;} and one or more return statements of the form \code{\RETURN{} $e$;}.
6296 6366
6297 6367
6298
6299
6300 \subsection{ Labels} 6368 \subsection{ Labels}
6301 \LMLabel{labels} 6369 \LMLabel{labels}
6302 6370
6303 \LMHash{} 6371 \LMHash{}
6304 A {\em label} is an identifier followed by a colon. A {\em labeled statement} is a statement prefixed by a label $L$. A {\em labeled case clause} is a case cla use within a switch statement (\ref{switch}) prefixed by a label $L$. 6372 A {\em label} is an identifier followed by a colon. A {\em labeled statement} is a statement prefixed by a label $L$. A {\em labeled case clause} is a case cla use within a switch statement (\ref{switch}) prefixed by a label $L$.
6305 6373
6306 \rationale{The sole role of labels is to provide targets for the break (\ref{bre ak}) and continue (\ref{continue}) statements.} 6374 \rationale{The sole role of labels is to provide targets for the break (\ref{bre ak}) and continue (\ref{continue}) statements.}
6307 6375
6308 %\Q{Are labels in a separate namespace? Bug 49774299} 6376 %\Q{Are labels in a separate namespace? Bug 49774299}
6309 6377
6310 \begin{grammar} 6378 \begin{grammar}
6311 {\bf label:} 6379 {\bf label:}
6312 identifier `{\escapegrammar :}' 6380 identifier `{\escapegrammar :}'
6313 . 6381 .
6314 \end{grammar} 6382 \end{grammar}
6315 6383
6316 \LMHash{} 6384 \LMHash{}
6317 The semantics of a labeled statement $L: s$ are identical to those of the state ment $s$. The namespace of labels is distinct from the one used for types, funct ions and variables. 6385 Execution a labeled statement $s$, $label: s_l$, consists of executing $s_l$.
6386 If execution of $s_l$ {\em breaks} with the label $label$,
6387 then execution of $s$ completes normally,
6388 otherwise execution of $s$ completes in the same ways as the execution of $s_l$.
6389
6390 The namespace of labels is distinct from the one used for types, functions and v ariables.
eernst 2016/10/19 13:37:07 Prepend `LMHash{}` to new paragraph.
Lasse Reichstein Nielsen 2016/10/31 16:54:44 It's not new - it's just separated from its origin
6318 6391
6319 \LMHash{} 6392 \LMHash{}
6320 The scope of a label that labels a statement $s$ is $s$. The scope of a label th at labels a case clause of a switch statement $s$ is $s$. 6393 The scope of a label that labels a statement $s$ is $s$. The scope of a label th at labels a case clause of a switch statement $s$ is $s$.
6321 6394
6322 \rationale{Labels should be avoided by programmers at all costs. The motivation for including labels in the language is primarily making Dart a better target fo r code generation. 6395 \rationale{Labels should be avoided by programmers at all costs. The motivation for including labels in the language is primarily making Dart a better target fo r code generation.
6323 } 6396 }
6324 6397
6325 6398
6326 \subsection{ Break} 6399 \subsection{ Break}
6327 \LMLabel{break} 6400 \LMLabel{break}
6328 6401
6329 \LMHash{} 6402 \LMHash{}
6330 The {\em break statement} consists of the reserved word \BREAK{} and an optional label (\ref{labels}). 6403 The {\em break statement} consists of the reserved word \BREAK{} and an optional label (\ref{labels}).
6331 6404
6332 \begin{grammar} 6405 \begin{grammar}
6333 {\bf breakStatement:} 6406 {\bf breakStatement:}
6334 \BREAK{} identifier? `{\escapegrammar ;}' 6407 \BREAK{} identifier? `{\escapegrammar ;}'
6335 . 6408 .
6336 \end{grammar} 6409 \end{grammar}
6337 6410
6338 \LMHash{} 6411 \LMHash{}
6339 Let $s_b$ be a \BREAK{} statement. If $s_b$ is of the form \code{\BREAK{} $L$;} , then let $s_E$ be the innermost labeled statement with label $L$ enclosing $s_ b$. If $s_b$ is of the form \code{\BREAK{};}, then let $s_E$ be the innermost \DO{} (\ref{do}), \FOR{} (\ref{for}), \SWITCH{} (\ref{switch}) or \WHILE{} (\ref {while}) statement enclosing $s_b$. It is a compile-time error if no such state ment $s_E$ exists within the innermost function in which $s_b$ occurs. Further more, let $s_1, \ldots, s_n$ be those \TRY{} statements that are both enclosed i n $s_E$ and that enclose $s_b$, and that have a \FINALLY{} clause. Lastly, let $f_j$ be the \FINALLY{} clause of $s_j, 1 \le j \le n$. Executing $s_b$ first executes $f_1, \ldots, f_n$ in innermost-clause-first order and then terminat es $s_E$. 6412 Let $s_b$ be a \BREAK{} statement. If $s_b$ is of the form \code{\BREAK{} $L$;} , then let $s_E$ be the innermost labeled statement with label $L$ enclosing $s_ b$. If $s_b$ is of the form \code{\BREAK{};}, then let $s_E$ be the innermost \DO{} (\ref{do}), \FOR{} (\ref{for}), \SWITCH{} (\ref{switch}) or \WHILE{} (\ref {while}) statement enclosing $s_b$. It is a compile-time error if no such state ment $s_E$ exists within the innermost function in which $s_b$ occurs.
6340 6413
6341 \LMHash{} 6414 \LMHash{}
6342 If $s_E$ is an asynchronous for loop (\ref{asynchronousFor-in}), its associated stream subscription is canceled. Furthermore, let $a_k$ be the set of asynchrono us for loops and yield-each statements (\ref{yieldEach}) enclosing $s_b$ that a re enclosed in $s_E , 1 \le k \le m$, where $a_k$ is enclosed in $a_{k+1}$. Th e stream subscriptions associated with $a_j$ are canceled, $1 \le j \le m$, inne rmost first, so that $a_j$ is canceled before $a_{j+1}$. 6415 Execution of a \BREAK{} statement \code{\BREAK{} label;} completes by {\em break ing} with the label \code{label} (\ref{completion}).
eernst 2016/10/19 13:37:07 `{\em breaking}` --> `breaking`. We have the same
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Done. Changed the label to $L$.
6343 6416
6417 Execution of a \BREAK{} statement \code{\BREAK{};} completes by {\em breaking} w ithout a label (\ref{completion}).
eernst 2016/10/19 13:37:06 `{\em breaking}` --> `breaking`.
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6344 6418
6345 6419
6346 \subsection{ Continue} 6420 \subsection{ Continue}
6347 \LMLabel{continue} 6421 \LMLabel{continue}
6348 6422
6349 \LMHash{} 6423 \LMHash{}
6350 The {\em continue statement} consists of the reserved word \CONTINUE{} and an op tional label (\ref{labels}). 6424 The {\em continue statement} consists of the reserved word \CONTINUE{} and an op tional label (\ref{labels}).
6351 6425
6352 \begin{grammar} 6426 \begin{grammar}
6353 {\bf continueStatement:} 6427 {\bf continueStatement:}
6354 \CONTINUE{} identifier? `{\escapegrammar ;}' 6428 \CONTINUE{} identifier? `{\escapegrammar ;}'
6355 . 6429 .
6356 \end{grammar} 6430 \end{grammar}
6357 6431
6358 \LMHash{} 6432 \LMHash{}
6359 Let $s_c$ be a \CONTINUE{} statement. If $s_c$ is of the form \code{\CONTINUE{ } $L$;}, then let $s_E$ be the innermost labeled \DO{} (\ref{do}), \FOR{} (\ref{ for}) or \WHILE{} (\ref{while}) statement or case clause with label $L$ enclosin g $s_c$. If $s_c$ is of the form \code{\CONTINUE{};} then let $s_E$ be the inne rmost \DO{} (\ref{do}), \FOR{} (\ref{for}) or \WHILE{} (\ref{while}) statement enclosing $s_c$. It is a compile-time error if no such statement or case clause $s_E$ exists within the innermost function in which $s_c$ occurs. Furthermore , let $s_1, \ldots, s_n$ be those \TRY{} statements that are both enclosed in $s _E$ and that enclose $s_c$, and that have a \FINALLY{} clause. Lastly, let $f_j $ be the \FINALLY{} clause of $s_j, 1 \le j \le n$. Executing $s_c$ first exe cutes $f_1, \ldots, f_n$ in innermost-clause-first order. Then, if $s_E$ is a case clause, control is transferred to the case clause. Otherwise, $s_E$ is nece ssarily a loop and execution resumes after the last statement in the loop body. 6433 Let $s_c$ be a \CONTINUE{} statement. If $s_c$ is of the form \code{\CONTINUE{} $L$;}, then let $s_E$ be the innermost labeled \DO{} (\ref{do}), \FOR{} (\ref{f or}) or \WHILE{} (\ref{while}) statement or case clause with label $L$ enclosing $s_c$. If $s_c$ is of the form \code{\CONTINUE{};} then let $s_E$ be the inner most \DO{} (\ref{do}), \FOR{} (\ref{for}) or \WHILE{} (\ref{while}) statement e nclosing $s_c$. It is a compile-time error if no such statement or case clause $s_E$ exists within the innermost function in which $s_c$ occurs.
6360 6434
6361 \commentary{ 6435 Execution of a \CONTINUE{} statement \code{\CONTINUE{} label;} completes by {\em continuing} with the label \code{label} (\ref{completion}).
eernst 2016/10/19 13:37:06 `continuing`
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6362 In a while loop, that would be the boolean expression before the body. In a do loop, it would be the boolean expression after the body. In a for loop, it would be the increment clause. In other words, execution continues to the next itera tion of the loop. 6436
6363 } 6437 Execution of a \CONTINUE{} statement \code{\CONTINUE{};} completes by {\em conti nuing} without a label (\ref{completion}).
eernst 2016/10/19 13:37:06 `continuing`
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6438
6439
6440 \subsection{ Yield and Yield-Each}
6441 \LMLabel{yieldAndYieldEach}
6442
6443 \subsubsection{ Yield}
6444 \LMLabel{yield}
6364 6445
6365 \LMHash{} 6446 \LMHash{}
6366 If $s_E$ is an asynchronous for loop (\ref{asynchronousFor-in}), let $a_k$ be t he set of asynchronous for loops and yield-each statements (\ref{yieldEach}) enc losing $s_c$ that are enclosed in $s_E , 1 \le k \le m$, where $a_k$ is enclosed in $a_{k+1}$. The stream subscriptions associated with $a_j$ are canceled, $1 \le j \le m$, innermost first, so that $a_j$ is canceled before $a_{j+1}$. 6447 The {\em yield statement} adds an element to the result of a generator function (\ref{functions}).
6367 6448
6368 \subsection{ Yield and Yield-Each} 6449 \begin{grammar}
6369 \LMLabel{yieldAndYieldEach}
6370
6371 \subsubsection{ Yield}
6372 \LMLabel{yield}
6373
6374 \LMHash{}
6375 The {\em yield statement} adds an element to the result of a generator function (\ref{functions}).
6376
6377 \begin{grammar}
6378 {\bf yieldStatement:} 6450 {\bf yieldStatement:}
6379 \YIELD{} expression `{\escapegrammar ;}' 6451 \YIELD{} expression `{\escapegrammar ;}'
6380 . 6452 .
6381 \end{grammar} 6453 \end{grammar}
6382 6454
6383 \LMHash{} 6455 \LMHash{}
6384 Execution of a statement $s$ of the form \code{\YIELD{} $e$;} proceeds as follo ws: 6456 Execution of a statement $s$ of the form \code{\YIELD{} $e$;} proceeds as follo ws:
6385 6457
6386 \LMHash{} 6458 \LMHash{}
6387 First, the expression $e$ is evaluated to an object $o$. If the enclosing functi on $m$ is marked \ASYNC* (\ref{functions}) and the stream $u$ associated with $m $ has been paused, then execution of $m$ is suspended until $u$ is resumed or c anceled. 6459 First, the expression $e$ is evaluated to an object $o$. If the enclosing functi on $m$ is marked \ASYNC* (\ref{functions}) and the stream $u$ associated with $m $ has been paused, then the nearest enclosing asynchronous for loop (\ref{asynch ronousFor-in}), if any, is paused and execution of $m$ is suspended until $u$ is resumed or canceled.
6388 6460
6389 \LMHash{} 6461 \LMHash{}
6390 Next, $o$ is added to the iterable or stream associated with the immediately enc losing function. 6462 Next, $o$ is added to the iterable or stream associated with the immediately enc losing function.
6391 6463
6392 \LMHash{} 6464 \LMHash{}
6393 If the enclosing function $m$ is marked \ASYNC* and the stream $u$ associated wi th $m$ has been canceled, then let $c$ be the \FINALLY{} clause (\ref{try}) of t he innermost enclosing try-finally statement, if any. If $c$ is defined, let $h$ be the handler induced by $c$. If $h$ is defined, control is transferred to $h$ . If $h$ is undefined, the immediately enclosing function terminates. 6465 If the enclosing function $m$ is marked \ASYNC* and the stream $u$ associated wi th $m$ has been canceled, then the \YIELD{} statement completes by {\em returnin g} \code{null} (\ref{completion}), otherwise it completes normally.
eernst 2016/10/19 13:37:07 `returning`
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6394 6466
6395 \rationale{ 6467 \rationale{
6396 The stream associated with an asynchronous generator could be canceled by any co de with a reference to that stream at any point where the generator was passivat ed. Such a cancellation constitutes an irretrievable error for the generator. A t this point, the only plausible action for the generator is to clean up after i tself via its \FINALLY{} clauses. 6468 The stream associated with an asynchronous generator could be canceled by any co de with a reference to that stream at any point where the generator was passivat ed. Such a cancellation constitutes an irretrievable error for the generator. A t this point, the only plausible action for the generator is to clean up after i tself via its \FINALLY{} clauses.
6397 } 6469 }
6398 6470
6399 \LMHash{} 6471 \LMHash{}
6400 Otherwise, if the enclosing function $m$ is marked \ASYNC* (\ref{functions}) the n the enclosing function may suspend. 6472 Otherwise, if the enclosing function $m$ is marked \ASYNC* (\ref{functions}) the n the enclosing function may suspend, in which case the nearest enclosing asynch ronous for loop (\ref{asynchronousFor-in}), if any, is paused first.
6401 6473
6402 \rationale { 6474 \rationale {
6403 If a \YIELD{} occurred inside an infinite loop and the enclosing function never suspended, there might not be an opportunity for consumers of the enclosing str eam to run and access the data in the stream. The stream might then accumulate an unbounded number of elements. Such a situation is untenable. Therefore, we al low the enclosing function to be suspended when a new value is added to its asso ciated stream. However, it is not essential (and in fact, can be quite costly) t o suspend the function on every \YIELD{}. The implementation is free to decide h ow often to suspend the enclosing function. The only requirement is that consume rs are not blocked indefinitely. 6475 If a \YIELD{} occurred inside an infinite loop and the enclosing function never suspended, there might not be an opportunity for consumers of the enclosing str eam to run and access the data in the stream. The stream might then accumulate an unbounded number of elements. Such a situation is untenable. Therefore, we al low the enclosing function to be suspended when a new value is added to its asso ciated stream. However, it is not essential (and in fact, can be quite costly) t o suspend the function on every \YIELD{}. The implementation is free to decide h ow often to suspend the enclosing function. The only requirement is that consume rs are not blocked indefinitely.
6404 } 6476 }
6405 6477
6406 6478
6407 \LMHash{} 6479 \LMHash{}
6408 If the enclosing function $m$ is marked \SYNC* (\ref{functions}) then: 6480 If the enclosing function $m$ is marked \SYNC* (\ref{functions}) then:
6409 \begin{itemize} 6481 \begin{itemize}
6410 \item 6482 \item
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
6443 6515
6444 \LMHash{} 6516 \LMHash{}
6445 First, the expression $e$ is evaluated to an object $o$. 6517 First, the expression $e$ is evaluated to an object $o$.
6446 6518
6447 \LMHash{} 6519 \LMHash{}
6448 If the immediately enclosing function $m$ is marked \SYNC* (\ref{functions}), th en: 6520 If the immediately enclosing function $m$ is marked \SYNC* (\ref{functions}), th en:
6449 \begin{enumerate} 6521 \begin{enumerate}
6450 \item It is a dynamic error if the class of $o$ does not implement \code{Iterabl e}. Otherwise 6522 \item It is a dynamic error if the class of $o$ does not implement \code{Iterabl e}. Otherwise
6451 \item The method \cd{iterator} is invoked upon $o$ returning an object $i$. 6523 \item The method \cd{iterator} is invoked upon $o$ returning an object $i$.
6452 \item \label{moveNext} The \cd{moveNext} method of $i$ is invoked on it with no arguments. If \cd{moveNext} returns \FALSE{} execution of $s$ is complete. Other wise 6524 \item \label{moveNext} The \cd{moveNext} method of $i$ is invoked on it with no arguments. If \cd{moveNext} returns \FALSE{} execution of $s$ is complete. Other wise
6453 \item The getter \cd{current} is invoked on $i$. If the invocation raises an exc eption $ex$, execution of $s$ throws $ex$. Otherwise, the result $x$ of the gett er invocation is added to the iterable associated with $m$. 6525 \item The getter \cd{current} is invoked on $i$. If the invocation {\em throws} an exception $ex$, execution of $s$ completes in the same way. Otherwise, the re sult $x$ of the getter invocation is added to the iterable associated with $m$.
eernst 2016/10/19 13:37:05 If I understand this right then `the invocation th
Lasse Reichstein Nielsen 2016/10/31 16:54:44 Reworded since the invocation is an expression, so
6454 Execution of the function $m$ immediately enclosing $s$ is suspended until the n ullary method \code{moveNext()} is invoked upon the iterator used to initiate th e current invocation of $m$, at which point execution of $s$ continues at \ref{m oveNext}. 6526 Execution of the function $m$ immediately enclosing $s$ is suspended until the n ullary method \code{moveNext()} is invoked upon the iterator used to initiate th e current invocation of $m$, at which point execution of $s$ continues at \ref{m oveNext}.
6455 \item 6527 \item
6456 The current call to \code{moveNext()} returns \TRUE. 6528 The current call to \code{moveNext()} returns \TRUE.
6457 \end{enumerate} 6529 \end{enumerate}
6458 6530
6459 \LMHash{} 6531 \LMHash{}
6460 If $m$ is marked \ASYNC* (\ref{functions}), then: 6532 If $m$ is marked \ASYNC* (\ref{functions}), then:
6461 \begin{itemize} 6533 \begin{itemize}
6462 \item It is a dynamic error if the class of $o$ does not implement \code{Stream }. Otherwise 6534 \item It is a dynamic error if the class of $o$ does not implement \code{Stream }. Otherwise
6463 \item For each element $x$ of $o$: 6535 \item The nearest enclosing asynchronous for loop (\ref{asynchronousFor-in}), if any, is paused.
6536 \item The $o$ stream is listened to, creating a subscription $s$, and for each e vent $x$, or error $e$ with stack trace $t$, of $s$:
6464 \begin{itemize} 6537 \begin{itemize}
6465 \item 6538 \item
6466 If the stream $u$ associated with $m$ has been paused, then execution of $m$ is suspended until $u$ is resumed or canceled. 6539 If the stream $u$ associated with $m$ has been paused, then execution of $m$ is suspended until $u$ is resumed or canceled.
6467 \item
6468 If the stream $u$ associated with $m$ has been canceled, then let $c$ be the \FI NALLY{} clause (\ref{try}) of the innermost enclosing try-finally statement, if any. If $c$ is defined, let $h$ be the handler induced by $c$. If $h$ is define d, control is transferred to $h$. If $h$ is undefined, the immediately enclosing function terminates.
6469 \item 6540 \item
6470 Otherwise, $x$ is added to the stream associated with $m$ in the order it appea rs in $o$. The function $m$ may suspend. 6541 If the stream $u$ associated with $m$ has been canceled,
6542 then $s$ is canceled by evaluating \code{\AWAIT{} v.cancel()} where $v$ is a fre sh variable referencing the stream subscription $s$.
6543 Then, if the cancel completed normally, the stream execution of $s$ {\em returns } with no value (\ref{completion}).
eernst 2016/10/19 13:37:06 `returns`
Lasse Reichstein Nielsen 2016/10/31 16:54:43 Done.
6544 \item
6545 Otherwise, $x$, or $e$ with $t$, are added to the stream associated with $m$ in the order they appears in $o$. The function $m$ may suspend.
6471 \end{itemize} 6546 \end{itemize}
6472 \item If the stream $o$ is done, execution of $s$ is complete. 6547 \item If the stream $o$ is done, execution of $s$ completes normally.
6473 \end{itemize} 6548 \end{itemize}
6474 6549
6475 6550
6476 \LMHash{} 6551 \LMHash{}
6477 It is a compile-time error if a yield-each statement appears in a function that is not a generator function. 6552 It is a compile-time error if a yield-each statement appears in a function that is not a generator function.
6478 6553
6479 \LMHash{} 6554 \LMHash{}
6480 Let $T$ be the static type of $e$ and let $f$ be the immediately enclosing funct ion. It is a static type warning if $T$ may not be assigned to the declared ret urn type of $f$. If $f$ is synchronous it is a static type warning if $T$ may not be assigned to \code{Iterable}. If $f$ is asynchronous it is a static type warning if $T$ may not be assigned to \code{Stream}. 6555 Let $T$ be the static type of $e$ and let $f$ be the immediately enclosing funct ion. It is a static type warning if $T$ may not be assigned to the declared ret urn type of $f$. If $f$ is synchronous it is a static type warning if $T$ may not be assigned to \code{Iterable}. If $f$ is asynchronous it is a static type warning if $T$ may not be assigned to \code{Stream}.
6481 6556
6482 6557
6483 \subsection{ Assert} 6558 \subsection{ Assert}
6484 \LMLabel{assert} 6559 \LMLabel{assert}
6485 6560
6486 \LMHash{} 6561 \LMHash{}
6487 An {\em assert statement} is used to disrupt normal execution if a given boolean condition does not hold. 6562 An {\em assert statement} is used to disrupt normal execution if a given boolean condition does not hold.
6488 6563
6489 \begin{grammar} 6564 \begin{grammar}
6490 {\bf assertStatement:} 6565 {\bf assertStatement:}
6491 assert `(' expression `)' `{\escapegrammar ;}' 6566 assert `(' expression `)' `{\escapegrammar ;}'
6492 . 6567 .
6493 \end{grammar} 6568 \end{grammar}
6494 6569
6495 \LMHash{} 6570 \LMHash{}
6496 The assert statement has no effect in production mode. In checked mode, executio n of an assert statement \code{\ASSERT{}($e$);} proceeds as follows: 6571 The assert statement has no effect in production mode. In checked mode, executio n of an assert statement \code{\ASSERT{}($e$);} proceeds as follows:
6497 6572
6498 \LMHash{} 6573 \LMHash{}
6499 The expression $e$ is evaluated to an object $o$. If the class of $o$ is a subty pe of \code{Function} then let $r$ be the result of invoking $o$ with no argumen ts. Otherwise, let $r$ be $o$. 6574 The expression $e$ is evaluated to an object $o$. If the class of $o$ is a subty pe of \code{Function} then let $r$ be the result of invoking $o$ with no argumen ts. Otherwise, let $r$ be $o$.
6500 It is a dynamic type error if $o$ is not of type \code{bool} or of type \code{Fu nction}, or if $r$ is not of type \code{bool}. If $r$ is \FALSE{}, we say that the assertion failed. If $r$ is \TRUE{}, we say that the assertion succeeded. If the assertion succeeded, execution of the assert statement is complete. If the assertion failed, an \code{AssertionError} is thrown. 6575 It is a dynamic type error if $o$ is not of type \code{bool} or of type \code{Fu nction}, or if $r$ is not of type \code{bool}. If $r$ is \FALSE{}, we say that the assertion failed. If $r$ is \TRUE{}, we say that the assertion succeeded. If the assertion succeeded, execution of the assert statement is complete. If the assertion failed, the execution {\em throws} an \code{AssertionError} with a sta ck trace corresponding to the \ASSERT{} statement.
eernst 2016/10/19 13:37:07 `the execution throws .. to the current execution
Lasse Reichstein Nielsen 2016/10/31 16:54:45 Done.
6501 6576
6502 %\Q{Might be cleaner to define it as \code{if (!$e$) \{\THROW{} \NEW{} Assertion Error();\}} (in checked mode only). 6577 %\Q{Might be cleaner to define it as \code{if (!$e$) \{\THROW{} \NEW{} Assertion Error();\}} (in checked mode only).
6503 %What about an error message as part of the assert?} 6578 %What about an error message as part of the assert?}
6504 6579
6505 \LMHash{} 6580 \LMHash{}
6506 It is a static type warning if the type of $e$ may not be assigned to either \ code{bool} or $() \rightarrow$ \code{bool}. 6581 It is a static type warning if the type of $e$ may not be assigned to either \ code{bool} or $() \rightarrow$ \code{bool}.
6507 6582
6508 \rationale{Why is this a statement, not a built in function call? Because it is handled magically so it has no effect and no overhead in production mode. Also, in the absence of final methods. one could not prevent it being overridden (thou gh there is no real harm in that). It cannot be viewed as a function call that is being optimized away because the argument might have side effects. 6583 \rationale{Why is this a statement, not a built in function call? Because it is handled magically so it has no effect and no overhead in production mode. Also, in the absence of final methods. one could not prevent it being overridden (thou gh there is no real harm in that). It cannot be viewed as a function call that is being optimized away because the argument might have side effects.
6509 } 6584 }
6510 6585
(...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after
7901 7976
7902 The invariant that each normative paragraph is associated with a line 7977 The invariant that each normative paragraph is associated with a line
7903 containing the text \LMHash{} should be maintained. Extra occurrences 7978 containing the text \LMHash{} should be maintained. Extra occurrences
7904 of \LMHash{} can be added if needed, e.g., in order to make 7979 of \LMHash{} can be added if needed, e.g., in order to make
7905 individual \item{}s in itemized lists addressable. Each \LM.. command 7980 individual \item{}s in itemized lists addressable. Each \LM.. command
7906 must occur on a separate line. \LMHash{} must occur immediately 7981 must occur on a separate line. \LMHash{} must occur immediately
7907 before the associated paragraph, and \LMLabel must occur immediately 7982 before the associated paragraph, and \LMLabel must occur immediately
7908 after the associated \section{}, \subsection{} etc. 7983 after the associated \section{}, \subsection{} etc.
7909 7984
7910 ---------------------------------------------------------------------- 7985 ----------------------------------------------------------------------
OLDNEW
« 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