diff options
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/manual.of | 76 |
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 | ||
| 224 | All chunks start with an implicit declaration @T{global *}, | 224 | All chunks start with an implicit declaration @T{global *}, |
| 225 | which declares all free names as global variables; | 225 | which declares all free names as global variables; |
| 226 | this implicit declaration becomes void inside the scope of any other | 226 | this preambular declaration becomes void inside the scope of any other |
| 227 | @Rw{global} declaration, regardless of the names being declared. | 227 | @Rw{global} declaration, |
| 228 | as the following example illustrates: | ||
| 228 | @verbatim{ | 229 | @verbatim{ |
| 229 | X = 1 -- Ok, global by default | 230 | X = 1 -- Ok, global by default |
| 230 | do | 231 | do |
| 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 | ||
| 234 | end | 235 | end |
| 235 | X = 2 -- Ok, global by default again | 236 | X = 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{ |
| 1112 | and break do else elseif end | 1113 | and break do else elseif end |
| 1113 | false for function goto if in | 1114 | false for function global goto if |
| 1114 | local nil not or repeat return | 1115 | in local nil not or repeat |
| 1115 | then true until while | 1116 | return then true until while |
| 1116 | } | 1117 | } |
| 1117 | 1118 | ||
| 1118 | Lua is a case-sensitive language: | 1119 | Lua 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 | } |
| 1658 | If present, an initial assignment has the same semantics | 1660 | If present, an initial assignment has the same semantics |
| 1659 | of a multiple assignment @see{assignment}. | 1661 | of a multiple assignment @see{assignment}. |
| @@ -1662,24 +1664,55 @@ Otherwise, all local variables are initialized with @nil. | |||
| 1662 | Each variable name may be postfixed by an attribute | 1664 | Each 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 | } |
| 1667 | There are two possible attributes: | 1669 | There 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} |
| 1670 | that is, a variable that cannot be assigned to | 1672 | that is, a variable that cannot be used as the left-hand side of an |
| 1671 | after its initialization; | 1673 | assignment, |
| 1672 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. | 1674 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. |
| 1673 | A list of variables can contain at most one to-be-closed variable. | 1675 | A list of variables can contain at most one to-be-closed variable. |
| 1674 | Only local variables can have the @id{close} attribute. | 1676 | Only local variables can have the @id{close} attribute. |
| 1675 | 1677 | ||
| 1678 | Lua offers also a collective declaration for global variables: | ||
| 1679 | @Produc{ | ||
| 1680 | @producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}} | ||
| 1681 | } | ||
| 1682 | This special form implicitly declares | ||
| 1683 | as globals all names not explicitly declared previously. | ||
| 1684 | In particular, | ||
| 1685 | @T{global * <const>} implicitly declares | ||
| 1686 | as read-only globals all names not explicitly declared previously; | ||
| 1687 | see the following example: | ||
| 1688 | @verbatim{ | ||
| 1689 | global X | ||
| 1690 | global * <const> | ||
| 1691 | print(math.pi) -- Ok, 'print' and 'math' are read-only | ||
| 1692 | X = 1 -- Ok, declared as read-write | ||
| 1693 | Y = 1 -- Error, Y is read-only | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | As noted in @See{globalenv}, | ||
| 1697 | all chunks start with an implicit declaration @T{global *}, | ||
| 1698 | but this preambular declaration becomes void inside | ||
| 1699 | the scope of any other @Rw{global} declaration. | ||
| 1700 | Therefore, a program that does not use global declarations | ||
| 1701 | or start with @T{global *} | ||
| 1702 | has free read-write access to any global; | ||
| 1703 | a program that starts with @T{global * <const>} | ||
| 1704 | has free read-only access to any global; | ||
| 1705 | and a program that starts with any other global declaration | ||
| 1706 | (e.g., @T{global none}) can only refer to declared variables. | ||
| 1707 | |||
| 1676 | Note that, for global variables, | 1708 | Note that, for global variables, |
| 1677 | the @emph{read-only} atribute is only a syntactical restriction: | 1709 | the effect of any declaration is only syntactical: |
| 1678 | @verbatim{ | 1710 | @verbatim{ |
| 1679 | global X <const> | 1711 | global X <const>, _G |
| 1680 | X = 1 -- ERROR | 1712 | X = 1 -- ERROR |
| 1681 | _ENV.X = 1 -- Ok | 1713 | _ENV.X = 1 -- Ok |
| 1682 | foo() -- 'foo' can freely change the global X | 1714 | _G.print(X) -- Ok |
| 1715 | foo() -- 'foo' can freely change any global | ||
| 1683 | } | 1716 | } |
| 1684 | 1717 | ||
| 1685 | A chunk is also a block @see{chunks}, | 1718 | A chunk is also a block @see{chunks}, |
| @@ -9453,7 +9486,12 @@ change between versions. | |||
| 9453 | @itemize{ | 9486 | @itemize{ |
| 9454 | 9487 | ||
| 9455 | @item{ | 9488 | @item{ |
| 9456 | The control variable in @Rw{for} loops are read only. | 9489 | The word @Rw{global} is a reserved word. |
| 9490 | Do not use it as a regular name. | ||
| 9491 | } | ||
| 9492 | |||
| 9493 | @item{ | ||
| 9494 | The control variable in @Rw{for} loops is read only. | ||
| 9457 | If you need to change it, | 9495 | If you need to change it, |
| 9458 | declare a local variable with the same name in the loop body. | 9496 | declare 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{;}}} |
