Skip to content

Commit 2563de7

Browse files
Sainanwell-in-that-case
authored andcommitted
Document private fields
1 parent 24eb0fa commit 2563de7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

docs/New Features/Object-Oriented Programming.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,49 @@ print(human.name) -- "John"
124124

125125
Note that if you have a local variable (or function parameter) called "parent", the `parent` expression will defer to it.
126126

127+
## Private Fields
128+
129+
Pluto allows you to specify if a field is 'public' or 'private'. Private fields can only be accessed by the class that defined them.
130+
131+
```pluto
132+
class Human
133+
public name
134+
private age
135+
136+
function __construct(name, age)
137+
self.name = name
138+
self.age = age
139+
end
140+
141+
function getAge()
142+
return self.age
143+
end
144+
end
145+
146+
local human = new Human("John", 42)
147+
print(human.name) -- "John"
148+
print(human:getAge()) -- 42
149+
print(human.age) -- nil
150+
```
151+
127152
## Constructor Promotion
128153

129154
Because a common task of `__construct` methods is to assign the value of arguments to table fields, Pluto provides a simple syntax to reduce this boilerplate:
130155

131156
```pluto
132157
class Human
133-
function __construct(public name)
158+
function __construct(public name, private age)
159+
end
160+
161+
function getAge()
162+
return self.age
134163
end
135164
end
136165
137-
local human = new Human("John")
166+
local human = new Human("John", 42)
138167
print(human.name) -- "John"
168+
print(human:getAge()) -- 42
169+
print(human.age) -- nil
139170
```
140171

141172
## Instanceof Operator

src/theme/pluto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Prism.languages.pluto = {
1313
},
1414
'boolean': /\b(?:true|false)\b/,
1515
'number': /\b([\d][\d_]+|(0x[a-f\d]+)|(0b[01]+))(?:\.[a-f\d]*)?(?:p[+-]?\d+)?\b|\b\d+(?:\.\B|(?:\.\d*)?(?:e[+-]?\d+)?\b)|\B\.\d+(?:e[+-]?\d+)?\b/i,
16-
'keyword': /\$|\b(?:and|as|class|pluto_class|enum|begin|break|do|else|elseif|end|for|function|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)\b/,
16+
'keyword': /\$|\b(?:and|as|class|pluto_class|enum|begin|break|do|else|elseif|end|for|function|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)\b/,
1717
'variable': /\b(?:self|parent|pluto_parent)\b/,
1818
'operator': [
1919
/[-+*%^&|#]|\/\/?|<[<=]?|>[>=]?|[=~]=?|\:=|\?|(?<!\w)\:|\?\.|instanceof/,

0 commit comments

Comments
 (0)