aboutsummaryrefslogtreecommitdiff
path: root/manual
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-18 11:43:43 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-18 11:43:43 -0300
commitabbae57c7844b1121e7251d56f681394f20c1821 (patch)
tree823201f4d2631fcae027117681553de80b2c96f0 /manual
parentf2c1531e6cacb10926158d8def5fa5841a0f357e (diff)
downloadlua-abbae57c7844b1121e7251d56f681394f20c1821.tar.gz
lua-abbae57c7844b1121e7251d56f681394f20c1821.tar.bz2
lua-abbae57c7844b1121e7251d56f681394f20c1821.zip
Variable attributes can prefix name list
In this format, the attribute applies to all names in the list; e.g. "global<const> print, require, math".
Diffstat (limited to 'manual')
-rw-r--r--manual/manual.of30
1 files changed, 17 insertions, 13 deletions
diff --git a/manual/manual.of b/manual/manual.of
index effb95da..eb97e853 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -1651,43 +1651,47 @@ Function calls are explained in @See{functioncall}.
1651Local and global variables can be declared anywhere inside a block. 1651Local and global variables can be declared anywhere inside a block.
1652The declaration for locals can include an initialization: 1652The declaration for locals can include an initialization:
1653@Produc{ 1653@Produc{
1654@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} 1654@producname{stat}@producbody{@Rw{local}
1655 attnamelist @bnfopt{@bnfter{=} explist}}
1655@producname{stat}@producbody{@Rw{global} attnamelist} 1656@producname{stat}@producbody{@Rw{global} attnamelist}
1656@producname{attnamelist}@producbody{
1657 @bnfNter{Name} @bnfopt{attrib}
1658 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
1659} 1657}
1660If present, an initial assignment has the same semantics 1658If present, an initial assignment has the same semantics
1661of a multiple assignment @see{assignment}. 1659of a multiple assignment @see{assignment}.
1662Otherwise, all local variables are initialized with @nil. 1660Otherwise, all local variables are initialized with @nil.
1663 1661
1664Each variable name may be postfixed by an attribute 1662The list of names may be prefixed by an attribute
1665(a name between angle brackets): 1663(a name between angle brackets)
1664and each variable name may be postfixed by an attribute:
1666@Produc{ 1665@Produc{
1666@producname{attnamelist}@producbody{
1667 @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib}
1668 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
1667@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} 1669@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
1668} 1670}
1671A prefixed attribute applies to all names in the list;
1672a postfixed attribute applies to its particular name.
1669There are two possible attributes: 1673There are two possible attributes:
1670@id{const}, which declares a @emph{constant} or @emph{read-only} variable, 1674@id{const}, which declares a @emph{constant} or @emph{read-only} variable,
1671@index{constant variable} 1675@index{constant variable}
1672that is, a variable that cannot be used as the left-hand side of an 1676that is, a variable that cannot be used as the left-hand side of an
1673assignment, 1677assignment,
1674and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. 1678and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
1675A list of variables can contain at most one to-be-closed variable.
1676Only local variables can have the @id{close} attribute. 1679Only local variables can have the @id{close} attribute.
1680A list of variables can contain at most one to-be-closed variable.
1677 1681
1678Lua offers also a collective declaration for global variables: 1682Lua offers also a collective declaration for global variables:
1679@Produc{ 1683@Produc{
1680@producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}} 1684@producname{stat}@producbody{@Rw{global} @bnfopt{attrib} @bnfter{*}}
1681} 1685}
1682This special form implicitly declares 1686This special form implicitly declares
1683as globals all names not explicitly declared previously. 1687as globals all names not explicitly declared previously.
1684In particular, 1688In particular,
1685@T{global * <const>} implicitly declares 1689@T{global<const> *} implicitly declares
1686as read-only globals all names not explicitly declared previously; 1690as read-only globals all names not explicitly declared previously;
1687see the following example: 1691see the following example:
1688@verbatim{ 1692@verbatim{
1689global X 1693global X
1690global * <const> 1694global<const> *
1691print(math.pi) -- Ok, 'print' and 'math' are read-only 1695print(math.pi) -- Ok, 'print' and 'math' are read-only
1692X = 1 -- Ok, declared as read-write 1696X = 1 -- Ok, declared as read-write
1693Y = 1 -- Error, Y is read-only 1697Y = 1 -- Error, Y is read-only
@@ -1700,7 +1704,7 @@ the scope of any other @Rw{global} declaration.
1700Therefore, a program that does not use global declarations 1704Therefore, a program that does not use global declarations
1701or start with @T{global *} 1705or start with @T{global *}
1702has free read-write access to any global; 1706has free read-write access to any global;
1703a program that starts with @T{global * <const>} 1707a program that starts with @T{global<const> *}
1704has free read-only access to any global; 1708has free read-only access to any global;
1705and a program that starts with any other global declaration 1709and a program that starts with any other global declaration
1706(e.g., @T{global none}) can only refer to declared variables. 1710(e.g., @T{global none}) can only refer to declared variables.
@@ -9620,11 +9624,11 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9620@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody 9624@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody
9621@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} 9625@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
9622@OrNL @Rw{global} attnamelist 9626@OrNL @Rw{global} attnamelist
9623@OrNL @Rw{global} @bnfter{*} @bnfopt{attrib} 9627@OrNL @Rw{global} @bnfopt{attrib} @bnfter{*}
9624} 9628}
9625 9629
9626@producname{attnamelist}@producbody{ 9630@producname{attnamelist}@producbody{
9627 @bnfNter{Name} @bnfopt{attrib} 9631 @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib}
9628 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} 9632 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
9629 9633
9630@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} 9634@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}