diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
| commit | be8120906304a8658fab998587b969e0e42f5650 (patch) | |
| tree | 81cf2d38522b10468d09763fc25d261486008197 /manual | |
| parent | e05590591410a5e007a1e3f1691f6c1cf9d8fe45 (diff) | |
| download | lua-be8120906304a8658fab998587b969e0e42f5650.tar.gz lua-be8120906304a8658fab998587b969e0e42f5650.tar.bz2 lua-be8120906304a8658fab998587b969e0e42f5650.zip | |
First implementation of global declarations
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/manual.of | 184 |
1 files changed, 110 insertions, 74 deletions
diff --git a/manual/manual.of b/manual/manual.of index 7cd0d4db..ace5d375 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -213,11 +213,88 @@ of a given value @seeF{type}. | |||
| 213 | 213 | ||
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | @sect2{globalenv| @title{Environments and the Global Environment} | 216 | @sect2{globalenv| @title{Scopes, Variables, and Environments} |
| 217 | @index{visibility} | ||
| 218 | |||
| 219 | A variable name refers to a global or a local variable according | ||
| 220 | to the declaration that is in context at that point of the code. | ||
| 221 | (For the purposes of this discussion, | ||
| 222 | a function's formal parameter is equivalent to a local variable.) | ||
| 223 | |||
| 224 | All chunks start with an implicit declaration @T{global *}, | ||
| 225 | which declares all free names as global variables; | ||
| 226 | this implicit declaration becomes void inside the scope of any other | ||
| 227 | @Rw{global} declaration, regardless of the names being declared. | ||
| 228 | @verbatim{ | ||
| 229 | X = 1 -- Ok, global by default | ||
| 230 | do | ||
| 231 | global Y -- voids implicit initial declaration | ||
| 232 | X = 1 -- ERROR, X not declared | ||
| 233 | Y = 1 -- Ok, Y declared as global | ||
| 234 | end | ||
| 235 | X = 2 -- Ok, global by default again | ||
| 236 | } | ||
| 237 | So, outside any global declaration, | ||
| 238 | Lua works as @x{global-by-default}. | ||
| 239 | Inside any global declaration, | ||
| 240 | Lua works without a default: | ||
| 241 | All variables must be declared. | ||
| 242 | |||
| 243 | Lua is a lexically scoped language. | ||
| 244 | The scope of a variable declaration begins at the first statement after | ||
| 245 | the declaration and lasts until the last non-void statement | ||
| 246 | of the innermost block that includes the declaration. | ||
| 247 | (@emph{Void statements} are labels and empty statements.) | ||
| 248 | |||
| 249 | A declaration shadows any declaration for the same name that | ||
| 250 | is in context at the point of the declaration. Inside this | ||
| 251 | shadow, any outer declaration for that name is void. | ||
| 252 | See the next example: | ||
| 253 | @verbatim{ | ||
| 254 | global print, x | ||
| 255 | x = 10 -- global variable | ||
| 256 | do -- new block | ||
| 257 | local x = x -- new 'x', with value 10 | ||
| 258 | print(x) --> 10 | ||
| 259 | x = x+1 | ||
| 260 | do -- another block | ||
| 261 | local x = x+1 -- another 'x' | ||
| 262 | print(x) --> 12 | ||
| 263 | end | ||
| 264 | print(x) --> 11 | ||
| 265 | end | ||
| 266 | print(x) --> 10 (the global one) | ||
| 267 | } | ||
| 268 | |||
| 269 | Notice that, in a declaration like @T{local x = x}, | ||
| 270 | the new @id{x} being declared is not in scope yet, | ||
| 271 | and so the @id{x} in the left-hand side refers to the outside variable. | ||
| 272 | |||
| 273 | Because of the @x{lexical scoping} rules, | ||
| 274 | local variables can be freely accessed by functions | ||
| 275 | defined inside their scope. | ||
| 276 | A local variable used by an inner function is called an @def{upvalue} | ||
| 277 | (or @emphx{external local variable}, or simply @emphx{external variable}) | ||
| 278 | inside the inner function. | ||
| 279 | |||
| 280 | Notice that each execution of a @Rw{local} statement | ||
| 281 | defines new local variables. | ||
| 282 | Consider the following example: | ||
| 283 | @verbatim{ | ||
| 284 | a = {} | ||
| 285 | local x = 20 | ||
| 286 | for i = 1, 10 do | ||
| 287 | local y = 0 | ||
| 288 | a[i] = function () y = y + 1; return x + y end | ||
| 289 | end | ||
| 290 | } | ||
| 291 | The loop creates ten closures | ||
| 292 | (that is, ten instances of the anonymous function). | ||
| 293 | Each of these closures uses a different @id{y} variable, | ||
| 294 | while all of them share the same @id{x}. | ||
| 217 | 295 | ||
| 218 | As we will discuss further in @refsec{variables} and @refsec{assignment}, | 296 | As we will discuss further in @refsec{variables} and @refsec{assignment}, |
| 219 | any reference to a free name | 297 | any reference to a global variable @id{var} |
| 220 | (that is, a name not bound to any declaration) @id{var} | ||
| 221 | is syntactically translated to @T{_ENV.var}. | 298 | is syntactically translated to @T{_ENV.var}. |
| 222 | Moreover, every chunk is compiled in the scope of | 299 | Moreover, every chunk is compiled in the scope of |
| 223 | an external local variable named @id{_ENV} @see{chunks}, | 300 | an external local variable named @id{_ENV} @see{chunks}, |
| @@ -225,12 +302,14 @@ so @id{_ENV} itself is never a free name in a chunk. | |||
| 225 | 302 | ||
| 226 | Despite the existence of this external @id{_ENV} variable and | 303 | Despite the existence of this external @id{_ENV} variable and |
| 227 | the translation of free names, | 304 | the translation of free names, |
| 228 | @id{_ENV} is a completely regular name. | 305 | @id{_ENV} is a regular name. |
| 229 | In particular, | 306 | In particular, |
| 230 | you can define new variables and parameters with that name. | 307 | you can define new variables and parameters with that name. |
| 231 | Each reference to a free name uses the @id{_ENV} that is | 308 | (However, you should not define @id{_ENV} as a global variable, |
| 232 | visible at that point in the program, | 309 | otherwise @T{_ENV.var} would translate to |
| 233 | following the usual visibility rules of Lua @see{visibility}. | 310 | @T{_ENV._ENV.var} and so on, in an infinite loop.) |
| 311 | Each reference to a global variable name uses the @id{_ENV} that is | ||
| 312 | visible at that point in the program. | ||
| 234 | 313 | ||
| 235 | Any table used as the value of @id{_ENV} is called an @def{environment}. | 314 | Any table used as the value of @id{_ENV} is called an @def{environment}. |
| 236 | 315 | ||
| @@ -244,8 +323,8 @@ When Lua loads a chunk, | |||
| 244 | the default value for its @id{_ENV} variable | 323 | the default value for its @id{_ENV} variable |
| 245 | is the global environment @seeF{load}. | 324 | is the global environment @seeF{load}. |
| 246 | Therefore, by default, | 325 | Therefore, by default, |
| 247 | free names in Lua code refer to entries in the global environment | 326 | global variables in Lua code refer to entries in the global environment |
| 248 | and, therefore, they are also called @def{global variables}. | 327 | and, therefore, they act as conventional global variables. |
| 249 | Moreover, all standard libraries are loaded in the global environment | 328 | Moreover, all standard libraries are loaded in the global environment |
| 250 | and some functions there operate on that environment. | 329 | and some functions there operate on that environment. |
| 251 | You can use @Lid{load} (or @Lid{loadfile}) | 330 | You can use @Lid{load} (or @Lid{loadfile}) |
| @@ -1198,17 +1277,15 @@ global variables, local variables, and table fields. | |||
| 1198 | 1277 | ||
| 1199 | A single name can denote a global variable or a local variable | 1278 | A single name can denote a global variable or a local variable |
| 1200 | (or a function's formal parameter, | 1279 | (or a function's formal parameter, |
| 1201 | which is a particular kind of local variable): | 1280 | which is a particular kind of local variable) @see{globalenv}: |
| 1202 | @Produc{ | 1281 | @Produc{ |
| 1203 | @producname{var}@producbody{@bnfNter{Name}} | 1282 | @producname{var}@producbody{@bnfNter{Name}} |
| 1204 | } | 1283 | } |
| 1205 | @bnfNter{Name} denotes identifiers @see{lexical}. | 1284 | @bnfNter{Name} denotes identifiers @see{lexical}. |
| 1206 | 1285 | ||
| 1207 | Any variable name is assumed to be global unless explicitly declared | 1286 | Because variables are @emph{lexically scoped}, |
| 1208 | as a local @see{localvar}. | ||
| 1209 | @x{Local variables} are @emph{lexically scoped}: | ||
| 1210 | local variables can be freely accessed by functions | 1287 | local variables can be freely accessed by functions |
| 1211 | defined inside their scope @see{visibility}. | 1288 | defined inside their scope @see{globalenv}. |
| 1212 | 1289 | ||
| 1213 | Before the first assignment to a variable, its value is @nil. | 1290 | Before the first assignment to a variable, its value is @nil. |
| 1214 | 1291 | ||
| @@ -1227,8 +1304,6 @@ The syntax @id{var.Name} is just syntactic sugar for | |||
| 1227 | 1304 | ||
| 1228 | An access to a global variable @id{x} | 1305 | An access to a global variable @id{x} |
| 1229 | is equivalent to @id{_ENV.x}. | 1306 | is equivalent to @id{_ENV.x}. |
| 1230 | Due to the way that chunks are compiled, | ||
| 1231 | the variable @id{_ENV} itself is never global @see{globalenv}. | ||
| 1232 | 1307 | ||
| 1233 | } | 1308 | } |
| 1234 | 1309 | ||
| @@ -1571,17 +1646,18 @@ Function calls are explained in @See{functioncall}. | |||
| 1571 | 1646 | ||
| 1572 | } | 1647 | } |
| 1573 | 1648 | ||
| 1574 | @sect3{localvar| @title{Local Declarations} | 1649 | @sect3{localvar| @title{Variable Declarations} |
| 1575 | @x{Local variables} can be declared anywhere inside a block. | 1650 | Local and global variables can be declared anywhere inside a block. |
| 1576 | The declaration can include an initialization: | 1651 | The declaration for locals can include an initialization: |
| 1577 | @Produc{ | 1652 | @Produc{ |
| 1578 | @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} | 1653 | @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} |
| 1654 | @producname{stat}@producbody{@Rw{global} attnamelist} | ||
| 1579 | @producname{attnamelist}@producbody{ | 1655 | @producname{attnamelist}@producbody{ |
| 1580 | @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} | 1656 | @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} |
| 1581 | } | 1657 | } |
| 1582 | If present, an initial assignment has the same semantics | 1658 | If present, an initial assignment has the same semantics |
| 1583 | of a multiple assignment @see{assignment}. | 1659 | of a multiple assignment @see{assignment}. |
| 1584 | Otherwise, all variables are initialized with @nil. | 1660 | Otherwise, all local variables are initialized with @nil. |
| 1585 | 1661 | ||
| 1586 | Each variable name may be postfixed by an attribute | 1662 | Each variable name may be postfixed by an attribute |
| 1587 | (a name between angle brackets): | 1663 | (a name between angle brackets): |
| @@ -1595,11 +1671,22 @@ that is, a variable that cannot be assigned to | |||
| 1595 | after its initialization; | 1671 | after its initialization; |
| 1596 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. | 1672 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. |
| 1597 | A list of variables can contain at most one to-be-closed variable. | 1673 | A list of variables can contain at most one to-be-closed variable. |
| 1674 | Only local variables can have the @id{close} attribute. | ||
| 1675 | |||
| 1676 | Note that, for global variables, | ||
| 1677 | the @emph{read-only} atribute is only a syntactical restriction: | ||
| 1678 | @verbatim{ | ||
| 1679 | global X <const> | ||
| 1680 | X = 1 -- ERROR | ||
| 1681 | _ENV.X = 1 -- Ok | ||
| 1682 | foo() -- 'foo' can freely change the global X | ||
| 1683 | } | ||
| 1598 | 1684 | ||
| 1599 | A chunk is also a block @see{chunks}, | 1685 | A chunk is also a block @see{chunks}, |
| 1600 | and so local variables can be declared in a chunk outside any explicit block. | 1686 | and so variables can be declared in a chunk outside any explicit block. |
| 1601 | 1687 | ||
| 1602 | The visibility rules for local variables are explained in @See{visibility}. | 1688 | The visibility rules for variable declarations |
| 1689 | are explained in @See{globalenv}. | ||
| 1603 | 1690 | ||
| 1604 | } | 1691 | } |
| 1605 | 1692 | ||
| @@ -2356,58 +2443,6 @@ return x,y,f() -- returns x, y, and all results from f(). | |||
| 2356 | 2443 | ||
| 2357 | } | 2444 | } |
| 2358 | 2445 | ||
| 2359 | @sect2{visibility| @title{Visibility Rules} | ||
| 2360 | |||
| 2361 | @index{visibility} | ||
| 2362 | Lua is a lexically scoped language. | ||
| 2363 | The scope of a local variable begins at the first statement after | ||
| 2364 | its declaration and lasts until the last non-void statement | ||
| 2365 | of the innermost block that includes the declaration. | ||
| 2366 | (@emph{Void statements} are labels and empty statements.) | ||
| 2367 | Consider the following example: | ||
| 2368 | @verbatim{ | ||
| 2369 | x = 10 -- global variable | ||
| 2370 | do -- new block | ||
| 2371 | local x = x -- new 'x', with value 10 | ||
| 2372 | print(x) --> 10 | ||
| 2373 | x = x+1 | ||
| 2374 | do -- another block | ||
| 2375 | local x = x+1 -- another 'x' | ||
| 2376 | print(x) --> 12 | ||
| 2377 | end | ||
| 2378 | print(x) --> 11 | ||
| 2379 | end | ||
| 2380 | print(x) --> 10 (the global one) | ||
| 2381 | } | ||
| 2382 | |||
| 2383 | Notice that, in a declaration like @T{local x = x}, | ||
| 2384 | the new @id{x} being declared is not in scope yet, | ||
| 2385 | and so the second @id{x} refers to the outside variable. | ||
| 2386 | |||
| 2387 | Because of the @x{lexical scoping} rules, | ||
| 2388 | local variables can be freely accessed by functions | ||
| 2389 | defined inside their scope. | ||
| 2390 | A local variable used by an inner function is called an @def{upvalue} | ||
| 2391 | (or @emphx{external local variable}, or simply @emphx{external variable}) | ||
| 2392 | inside the inner function. | ||
| 2393 | |||
| 2394 | Notice that each execution of a @Rw{local} statement | ||
| 2395 | defines new local variables. | ||
| 2396 | Consider the following example: | ||
| 2397 | @verbatim{ | ||
| 2398 | a = {} | ||
| 2399 | local x = 20 | ||
| 2400 | for i = 1, 10 do | ||
| 2401 | local y = 0 | ||
| 2402 | a[i] = function () y = y + 1; return x + y end | ||
| 2403 | end | ||
| 2404 | } | ||
| 2405 | The loop creates ten closures | ||
| 2406 | (that is, ten instances of the anonymous function). | ||
| 2407 | Each of these closures uses a different @id{y} variable, | ||
| 2408 | while all of them share the same @id{x}. | ||
| 2409 | |||
| 2410 | } | ||
| 2411 | 2446 | ||
| 2412 | } | 2447 | } |
| 2413 | 2448 | ||
| @@ -9535,6 +9570,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) | |||
| 9535 | @OrNL @Rw{function} funcname funcbody | 9570 | @OrNL @Rw{function} funcname funcbody |
| 9536 | @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody | 9571 | @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody |
| 9537 | @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} | 9572 | @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} |
| 9573 | @OrNL @Rw{global} attnamelist | ||
| 9538 | } | 9574 | } |
| 9539 | 9575 | ||
| 9540 | @producname{attnamelist}@producbody{ | 9576 | @producname{attnamelist}@producbody{ |
