Private fields and methods
Put private in front of a field or a method to keep it to the type. Only the type's own methods, its construct block and (for a component) its render can use it.
<type name="Counter">
<construct>
<field name="start" type="int" default="0" />
<do>.count = .start</do>
</construct>
<field name="count" type="int" private="yes" />
<fn name="doubled" value=".count * 2" private="yes" />
<fn name="total" value="$this->doubled() + .count" />
</type>
<let name="counter" value="Counter(start: 2)" />
<echo value="$counter->total()" />type Counter {
construct({ start: int = 0 }) {
.count = .start
}
private count: int
private fn doubled() => .count * 2
fn total() => $this->doubled() + .count
}
$counter = Counter(start: 2)
echo $counter->total()- Code outside the type can't read a private field (
$counter.count), set it, pass it when a value is made, or call a private method ($counter->doubled()).kscript checkreports each, and the same error stops a running script. - A private field is left out when a value is shown or turned into JSON, so
$counter->convert(to: json)is{"start":2}. - A method reaches the private members of another value of its own type, such as
fn same(other: Counter) => $other.count == $count. - A joined type uses the private members of the types it is built on.
- The editor offers private members only inside the type.
- On a model a private field is still a column. It is stored and loaded as usual, and only the model's own methods read it.
- There is no
public, because it is what every other member already is.
private is about what other code may use. It isn't secrecy: the state of a live component travels to the browser and back, signed, whether it is private or not.