Skip to content

Commit 7187e85

Browse files
committed
Document 'global' keyword and implicit-global warning
1 parent ec91706 commit 7187e85

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

docs/New Features/Compiler Warnings.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ switch a do
178178
end
179179
```
180180

181+
### implicit-global
182+
This is raised when the `global` keyword is enabled and a global was declared without it. See [Explicit Globals](<Explicit Globals>).
183+
```pluto showLineNumbers
184+
pluto_use global
185+
186+
a = 1
187+
```
188+
```
189+
file.pluto:3: warning: implicit global creation [implicit-global]
190+
3 | a = 1
191+
| ^^^^^ here: prefix this with 'global' if creating a global was intended
192+
```
193+
181194
### discarded-return
182195
This is raised when the return value of a function declared `<nodiscard>` was discarded. See [Nodiscard Functions](<Nodiscard Functions>).
183196
```pluto showLineNumbers
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
The optional `global` keyword can be used to explicitly declare global variables.
6+
7+
To enable it, simply do:
8+
```pluto
9+
pluto_use global
10+
```
11+
12+
Then it can be used like this:
13+
```pluto
14+
global a = 1
15+
```
16+
17+
Being identical to the following code:
18+
```pluto
19+
a = 1
20+
```
21+
22+
## Compiler Warnings
23+
24+
When the `global` keyword is enabled, an 'implicit-global' warning is raised for any locals declared without it:
25+
26+
```pluto showLineNumbers
27+
pluto_use global
28+
29+
a = 1
30+
```
31+
```
32+
file.pluto:3: warning: implicit global creation [implicit-global]
33+
3 | a = 1
34+
| ^^^^^ here: prefix this with 'global' if creating a global was intended
35+
```
36+
37+
## `pluto_use` Shorthand
38+
39+
Instead of having to do this:
40+
```pluto
41+
pluto_use "0.8.0", global
42+
```
43+
You can use a '+' after the version number to not only enable all non-compatible keywords, but also all optional keywords. In this case, the only optional keyword as of Pluto 0.9.0 is `global`, so the above and the following statement are identical:
44+
```pluto
45+
pluto_use "0.9.0+"
46+
```

src/theme/pluto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Prism.languages.pluto = {
22
'comment': /^#!.+|--(?:\[(=*)\[[\s\S]*?\]\1\]|.*)/m,
33
'attr-name': /(?<=(function|class|extends)\s)[\w:]+/,
4-
'keyword': /\$(define\b)?|\b(?:and|as|class|pluto_class|enum|begin|break|do|else|elseif|end|for|goto|if|in|local|new|not|or|repeat|return|static|then|until|while|continue|switch|case|default|pluto_switch|pluto_continue|extends|export|pluto_export|pluto_use|public|private|try|catch|pluto_try|pluto_catch)\b/,
4+
'keyword': /\$(define\b)?|\b(?:and|as|class|pluto_class|enum|begin|break|do|else|elseif|end|for|goto|if|in|local|new|not|or|repeat|return|static|then|until|while|continue|switch|case|default|pluto_switch|pluto_continue|extends|export|pluto_export|pluto_use|public|private|try|catch|pluto_try|pluto_catch|global)\b/,
55
'function': [
66
/\b(?!in\s)(?!\d)(?!return)(?!case)(?!function)(?!local)(?!new)\w+(?=\s*(?:\??\())/, // func()
77
/\b(?!in\s)(?!\d)(?!return)(?!case)(?!function)(?!local)(?!not)\w+(?=\s*(?:\??[{"]))/, // func "", func {}

0 commit comments

Comments
 (0)