diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-07-08 13:33:57 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-07-08 13:33:57 -0300 |
| commit | 942c10a5e33811a08a290ec15031c950a6d17c99 (patch) | |
| tree | 114395a69857609bdd6c220f85476812f0fd5fb1 /manual/manual.of | |
| parent | 848568790826b7e201f84682185b5b605c473016 (diff) | |
| download | lua-942c10a5e33811a08a290ec15031c950a6d17c99.tar.gz lua-942c10a5e33811a08a290ec15031c950a6d17c99.tar.bz2 lua-942c10a5e33811a08a290ec15031c950a6d17c99.zip | |
Optional initialization for global declarations
Diffstat (limited to '')
| -rw-r--r-- | manual/manual.of | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/manual/manual.of b/manual/manual.of index bcc8173b..8f90f942 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -229,7 +229,7 @@ as the following example illustrates: | |||
| 229 | @verbatim{ | 229 | @verbatim{ |
| 230 | X = 1 -- Ok, global by default | 230 | X = 1 -- Ok, global by default |
| 231 | do | 231 | do |
| 232 | global Y -- voids implicit initial declaration | 232 | global Y -- voids the implicit initial declaration |
| 233 | Y = 1 -- Ok, Y declared as global | 233 | Y = 1 -- Ok, Y declared as global |
| 234 | X = 1 -- ERROR, X not declared | 234 | X = 1 -- ERROR, X not declared |
| 235 | end | 235 | end |
| @@ -269,7 +269,7 @@ print(x) --> 10 (the global one) | |||
| 269 | 269 | ||
| 270 | Notice that, in a declaration like @T{local x = x}, | 270 | Notice that, in a declaration like @T{local x = x}, |
| 271 | the new @id{x} being declared is not in scope yet, | 271 | the new @id{x} being declared is not in scope yet, |
| 272 | and so the @id{x} in the left-hand side refers to the outside variable. | 272 | and so the @id{x} in the right-hand side refers to the outside variable. |
| 273 | 273 | ||
| 274 | Because of the @x{lexical scoping} rules, | 274 | Because of the @x{lexical scoping} rules, |
| 275 | local variables can be freely accessed by functions | 275 | local variables can be freely accessed by functions |
| @@ -1651,11 +1651,12 @@ Function calls are explained in @See{functioncall}. | |||
| 1651 | 1651 | ||
| 1652 | @sect3{localvar| @title{Variable Declarations} | 1652 | @sect3{localvar| @title{Variable Declarations} |
| 1653 | Local and global variables can be declared anywhere inside a block. | 1653 | Local and global variables can be declared anywhere inside a block. |
| 1654 | The declaration for locals can include an initialization: | 1654 | The declaration can include an initialization: |
| 1655 | @Produc{ | 1655 | @Produc{ |
| 1656 | @producname{stat}@producbody{@Rw{local} | 1656 | @producname{stat}@producbody{@Rw{local} |
| 1657 | attnamelist @bnfopt{@bnfter{=} explist}} | 1657 | attnamelist @bnfopt{@bnfter{=} explist}} |
| 1658 | @producname{stat}@producbody{@Rw{global} attnamelist} | 1658 | @producname{stat}@producbody{@Rw{global} |
| 1659 | attnamelist @bnfopt{@bnfter{=} explist}} | ||
| 1659 | } | 1660 | } |
| 1660 | If present, an initial assignment has the same semantics | 1661 | If present, an initial assignment has the same semantics |
| 1661 | of a multiple assignment @see{assignment}. | 1662 | of a multiple assignment @see{assignment}. |
| @@ -1712,7 +1713,8 @@ and a program that starts with any other global declaration | |||
| 1712 | (e.g., @T{global none}) can only refer to declared variables. | 1713 | (e.g., @T{global none}) can only refer to declared variables. |
| 1713 | 1714 | ||
| 1714 | Note that, for global variables, | 1715 | Note that, for global variables, |
| 1715 | the effect of any declaration is only syntactical: | 1716 | the effect of any declaration is only syntactical |
| 1717 | (except for the optional assignment): | ||
| 1716 | @verbatim{ | 1718 | @verbatim{ |
| 1717 | global X <const>, _G | 1719 | global X <const>, _G |
| 1718 | X = 1 -- ERROR | 1720 | X = 1 -- ERROR |
| @@ -3924,8 +3926,8 @@ This macro may evaluate its arguments more than once. | |||
| 3924 | 3926 | ||
| 3925 | } | 3927 | } |
| 3926 | 3928 | ||
| 3927 | @APIEntry{unsigned (lua_numbertocstring) (lua_State *L, int idx, | 3929 | @APIEntry{unsigned lua_numbertocstring (lua_State *L, int idx, |
| 3928 | char *buff);| | 3930 | char *buff);| |
| 3929 | @apii{0,0,-} | 3931 | @apii{0,0,-} |
| 3930 | 3932 | ||
| 3931 | Converts the number at acceptable index @id{idx} to a string | 3933 | Converts the number at acceptable index @id{idx} to a string |
| @@ -4050,7 +4052,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. | |||
| 4050 | 4052 | ||
| 4051 | } | 4053 | } |
| 4052 | 4054 | ||
| 4053 | @APIEntry{const char *(lua_pushexternalstring) (lua_State *L, | 4055 | @APIEntry{const char *lua_pushexternalstring (lua_State *L, |
| 4054 | const char *s, size_t len, lua_Alloc falloc, void *ud);| | 4056 | const char *s, size_t len, lua_Alloc falloc, void *ud);| |
| 4055 | @apii{0,1,m} | 4057 | @apii{0,1,m} |
| 4056 | 4058 | ||
