diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-11-08 11:43:42 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-11-08 11:43:42 -0300 |
| commit | e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa (patch) | |
| tree | 090174ab9a66fbec195e9dd539710ab19370ec2f /manual | |
| parent | f791bb69061c15f73395c5a95958ac18af5ef764 (diff) | |
| download | lua-e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa.tar.gz lua-e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa.tar.bz2 lua-e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa.zip | |
Global initialization checks name conflict
Initialization "global a = 10" raises an error if global 'a' is already
defined, that is, it has a non-nil value.
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/manual.of | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/manual/manual.of b/manual/manual.of index 0127df02..eaf0ce78 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -1660,9 +1660,15 @@ The declaration can include an initialization: | |||
| 1660 | @producname{stat}@producbody{@Rw{global} | 1660 | @producname{stat}@producbody{@Rw{global} |
| 1661 | attnamelist @bnfopt{@bnfter{=} explist}} | 1661 | attnamelist @bnfopt{@bnfter{=} explist}} |
| 1662 | } | 1662 | } |
| 1663 | If present, an initial assignment has the same semantics | 1663 | If there is no initialization, |
| 1664 | local variables are initialized with @nil; | ||
| 1665 | global variables are left unchanged. | ||
| 1666 | Otherwise, the initialization gets the same adjustment | ||
| 1664 | of a multiple assignment @see{assignment}. | 1667 | of a multiple assignment @see{assignment}. |
| 1665 | Otherwise, all local variables are initialized with @nil. | 1668 | Moreover, for global variables, |
| 1669 | the initialization will raise a runtime error | ||
| 1670 | if the variable is already defined, | ||
| 1671 | that is, it has a non-nil value. | ||
| 1666 | 1672 | ||
| 1667 | The list of names may be prefixed by an attribute | 1673 | The list of names may be prefixed by an attribute |
| 1668 | (a name between angle brackets) | 1674 | (a name between angle brackets) |
| @@ -2312,8 +2318,10 @@ global function f () @rep{body} end | |||
| 2312 | } | 2318 | } |
| 2313 | translates to | 2319 | translates to |
| 2314 | @verbatim{ | 2320 | @verbatim{ |
| 2315 | global f; f = function () @rep{body} end | 2321 | global f; global f = function () @rep{body} end |
| 2316 | } | 2322 | } |
| 2323 | The second @Rw{global} makes the assignment an initialization, | ||
| 2324 | which will raise an error if that global is already defined. | ||
| 2317 | 2325 | ||
| 2318 | The @emphx{colon} syntax | 2326 | The @emphx{colon} syntax |
| 2319 | is used to emulate @def{methods}, | 2327 | is used to emulate @def{methods}, |
