aboutsummaryrefslogtreecommitdiff
path: root/manual/manual.of
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-13 11:43:10 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-13 11:43:10 -0300
commit3b9dd52be02fd43c598f4adb6fa7844e6a573923 (patch)
tree8f858942f5db0e3d9d7cad503b59ee1541a60aa6 /manual/manual.of
parent7dc6aae29057c9dc4588f780c7abd72a62ff4c8e (diff)
downloadlua-3b9dd52be02fd43c598f4adb6fa7844e6a573923.tar.gz
lua-3b9dd52be02fd43c598f4adb6fa7844e6a573923.tar.bz2
lua-3b9dd52be02fd43c598f4adb6fa7844e6a573923.zip
Collective declaration for globals ('global *')
Diffstat (limited to 'manual/manual.of')
-rw-r--r--manual/manual.of76
1 files changed, 58 insertions, 18 deletions
diff --git a/manual/manual.of b/manual/manual.of
index cc71aaad..effb95da 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -223,14 +223,15 @@ a function's formal parameter is equivalent to a local variable.)
223 223
224All chunks start with an implicit declaration @T{global *}, 224All chunks start with an implicit declaration @T{global *},
225which declares all free names as global variables; 225which declares all free names as global variables;
226this implicit declaration becomes void inside the scope of any other 226this preambular declaration becomes void inside the scope of any other
227@Rw{global} declaration, regardless of the names being declared. 227@Rw{global} declaration,
228as the following example illustrates:
228@verbatim{ 229@verbatim{
229X = 1 -- Ok, global by default 230X = 1 -- Ok, global by default
230do 231do
231 global Y -- voids implicit initial declaration 232 global Y -- voids implicit initial declaration
232 X = 1 -- ERROR, X not declared
233 Y = 1 -- Ok, Y declared as global 233 Y = 1 -- Ok, Y declared as global
234 X = 1 -- ERROR, X not declared
234end 235end
235X = 2 -- Ok, global by default again 236X = 2 -- Ok, global by default again
236} 237}
@@ -1110,9 +1111,9 @@ and cannot be used as names:
1110@index{reserved words} 1111@index{reserved words}
1111@verbatim{ 1112@verbatim{
1112and break do else elseif end 1113and break do else elseif end
1113false for function goto if in 1114false for function global goto if
1114local nil not or repeat return 1115in local nil not or repeat
1115then true until while 1116return then true until while
1116} 1117}
1117 1118
1118Lua is a case-sensitive language: 1119Lua is a case-sensitive language:
@@ -1653,7 +1654,8 @@ The declaration for locals can include an initialization:
1653@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} 1654@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}}
1654@producname{stat}@producbody{@Rw{global} attnamelist} 1655@producname{stat}@producbody{@Rw{global} attnamelist}
1655@producname{attnamelist}@producbody{ 1656@producname{attnamelist}@producbody{
1656 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} 1657 @bnfNter{Name} @bnfopt{attrib}
1658 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
1657} 1659}
1658If present, an initial assignment has the same semantics 1660If present, an initial assignment has the same semantics
1659of a multiple assignment @see{assignment}. 1661of a multiple assignment @see{assignment}.
@@ -1662,24 +1664,55 @@ Otherwise, all local variables are initialized with @nil.
1662Each variable name may be postfixed by an attribute 1664Each variable name may be postfixed by an attribute
1663(a name between angle brackets): 1665(a name between angle brackets):
1664@Produc{ 1666@Produc{
1665@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} 1667@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
1666} 1668}
1667There are two possible attributes: 1669There are two possible attributes:
1668@id{const}, which declares a @emph{constant} or @emph{read-only} variable, 1670@id{const}, which declares a @emph{constant} or @emph{read-only} variable,
1669@index{constant variable} 1671@index{constant variable}
1670that is, a variable that cannot be assigned to 1672that is, a variable that cannot be used as the left-hand side of an
1671after its initialization; 1673assignment,
1672and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. 1674and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
1673A list of variables can contain at most one to-be-closed variable. 1675A list of variables can contain at most one to-be-closed variable.
1674Only local variables can have the @id{close} attribute. 1676Only local variables can have the @id{close} attribute.
1675 1677
1678Lua offers also a collective declaration for global variables:
1679@Produc{
1680@producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}}
1681}
1682This special form implicitly declares
1683as globals all names not explicitly declared previously.
1684In particular,
1685@T{global * <const>} implicitly declares
1686as read-only globals all names not explicitly declared previously;
1687see the following example:
1688@verbatim{
1689global X
1690global * <const>
1691print(math.pi) -- Ok, 'print' and 'math' are read-only
1692X = 1 -- Ok, declared as read-write
1693Y = 1 -- Error, Y is read-only
1694}
1695
1696As noted in @See{globalenv},
1697all chunks start with an implicit declaration @T{global *},
1698but this preambular declaration becomes void inside
1699the scope of any other @Rw{global} declaration.
1700Therefore, a program that does not use global declarations
1701or start with @T{global *}
1702has free read-write access to any global;
1703a program that starts with @T{global * <const>}
1704has free read-only access to any global;
1705and a program that starts with any other global declaration
1706(e.g., @T{global none}) can only refer to declared variables.
1707
1676Note that, for global variables, 1708Note that, for global variables,
1677the @emph{read-only} atribute is only a syntactical restriction: 1709the effect of any declaration is only syntactical:
1678@verbatim{ 1710@verbatim{
1679global X <const> 1711global X <const>, _G
1680X = 1 -- ERROR 1712X = 1 -- ERROR
1681_ENV.X = 1 -- Ok 1713_ENV.X = 1 -- Ok
1682foo() -- 'foo' can freely change the global X 1714_G.print(X) -- Ok
1715foo() -- 'foo' can freely change any global
1683} 1716}
1684 1717
1685A chunk is also a block @see{chunks}, 1718A chunk is also a block @see{chunks},
@@ -9453,7 +9486,12 @@ change between versions.
9453@itemize{ 9486@itemize{
9454 9487
9455@item{ 9488@item{
9456The control variable in @Rw{for} loops are read only. 9489The word @Rw{global} is a reserved word.
9490Do not use it as a regular name.
9491}
9492
9493@item{
9494The control variable in @Rw{for} loops is read only.
9457If you need to change it, 9495If you need to change it,
9458declare a local variable with the same name in the loop body. 9496declare a local variable with the same name in the loop body.
9459} 9497}
@@ -9582,12 +9620,14 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9582@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody 9620@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody
9583@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} 9621@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
9584@OrNL @Rw{global} attnamelist 9622@OrNL @Rw{global} attnamelist
9623@OrNL @Rw{global} @bnfter{*} @bnfopt{attrib}
9585} 9624}
9586 9625
9587@producname{attnamelist}@producbody{ 9626@producname{attnamelist}@producbody{
9588 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} 9627 @bnfNter{Name} @bnfopt{attrib}
9628 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
9589 9629
9590@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} 9630@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
9591 9631
9592@producname{retstat}@producbody{@Rw{return} 9632@producname{retstat}@producbody{@Rw{return}
9593 @bnfopt{explist} @bnfopt{@bnfter{;}}} 9633 @bnfopt{explist} @bnfopt{@bnfter{;}}}