diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-18 11:43:43 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-18 11:43:43 -0300 |
commit | abbae57c7844b1121e7251d56f681394f20c1821 (patch) | |
tree | 823201f4d2631fcae027117681553de80b2c96f0 /manual | |
parent | f2c1531e6cacb10926158d8def5fa5841a0f357e (diff) | |
download | lua-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.of | 30 |
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}. | |||
1651 | Local and global variables can be declared anywhere inside a block. | 1651 | Local and global variables can be declared anywhere inside a block. |
1652 | The declaration for locals can include an initialization: | 1652 | The 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 | } |
1660 | If present, an initial assignment has the same semantics | 1658 | If present, an initial assignment has the same semantics |
1661 | of a multiple assignment @see{assignment}. | 1659 | of a multiple assignment @see{assignment}. |
1662 | Otherwise, all local variables are initialized with @nil. | 1660 | Otherwise, all local variables are initialized with @nil. |
1663 | 1661 | ||
1664 | Each variable name may be postfixed by an attribute | 1662 | The list of names may be prefixed by an attribute |
1665 | (a name between angle brackets): | 1663 | (a name between angle brackets) |
1664 | and 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 | } |
1671 | A prefixed attribute applies to all names in the list; | ||
1672 | a postfixed attribute applies to its particular name. | ||
1669 | There are two possible attributes: | 1673 | There 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} |
1672 | that is, a variable that cannot be used as the left-hand side of an | 1676 | that is, a variable that cannot be used as the left-hand side of an |
1673 | assignment, | 1677 | assignment, |
1674 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. | 1678 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. |
1675 | A list of variables can contain at most one to-be-closed variable. | ||
1676 | Only local variables can have the @id{close} attribute. | 1679 | Only local variables can have the @id{close} attribute. |
1680 | A list of variables can contain at most one to-be-closed variable. | ||
1677 | 1681 | ||
1678 | Lua offers also a collective declaration for global variables: | 1682 | Lua 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 | } |
1682 | This special form implicitly declares | 1686 | This special form implicitly declares |
1683 | as globals all names not explicitly declared previously. | 1687 | as globals all names not explicitly declared previously. |
1684 | In particular, | 1688 | In particular, |
1685 | @T{global * <const>} implicitly declares | 1689 | @T{global<const> *} implicitly declares |
1686 | as read-only globals all names not explicitly declared previously; | 1690 | as read-only globals all names not explicitly declared previously; |
1687 | see the following example: | 1691 | see the following example: |
1688 | @verbatim{ | 1692 | @verbatim{ |
1689 | global X | 1693 | global X |
1690 | global * <const> | 1694 | global<const> * |
1691 | print(math.pi) -- Ok, 'print' and 'math' are read-only | 1695 | print(math.pi) -- Ok, 'print' and 'math' are read-only |
1692 | X = 1 -- Ok, declared as read-write | 1696 | X = 1 -- Ok, declared as read-write |
1693 | Y = 1 -- Error, Y is read-only | 1697 | Y = 1 -- Error, Y is read-only |
@@ -1700,7 +1704,7 @@ the scope of any other @Rw{global} declaration. | |||
1700 | Therefore, a program that does not use global declarations | 1704 | Therefore, a program that does not use global declarations |
1701 | or start with @T{global *} | 1705 | or start with @T{global *} |
1702 | has free read-write access to any global; | 1706 | has free read-write access to any global; |
1703 | a program that starts with @T{global * <const>} | 1707 | a program that starts with @T{global<const> *} |
1704 | has free read-only access to any global; | 1708 | has free read-only access to any global; |
1705 | and a program that starts with any other global declaration | 1709 | and 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{>}} |