Scope modifiers: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: Fix comments: Perl 6 --> Raku) |
(Added Wren) |
||
Line 1,624: | Line 1,624: | ||
Here, <code>std-cat</code> refers to the concatenation function from the standard |
Here, <code>std-cat</code> refers to the concatenation function from the standard |
||
library, not the locally declared constant by that name. |
library, not the locally declared constant by that name. |
||
=={{header|Wren}}== |
|||
Wren uses lexical scoping and has no scope modifiers as such. |
|||
However, instance and static fields of a class always begin respectively with one underscore or two underscores (nothing else can) and are always private to the class. |
|||
In the following example, the attempt to access the private field is picked up at compile time (Wren has a single-pass 'bytecode' compiler) and so the penultimate line, although OK, doesn't print. |
|||
<lang ecmascript>class MyClass { |
|||
construct new(a) { |
|||
_a = a // creates an instance field _a automatically |
|||
} |
|||
a { _a } // allow public access to the field |
|||
} |
|||
var mc = MyClass.new(3) |
|||
System.print(mc.a) // fine |
|||
System.print(mc._a) // can't access _a directly as its private to the class</lang> |
|||
{{out}} |
|||
<pre> |
|||
$ wren scope_modifiers.wren |
|||
[./scope_modifiers line 10] Error at '_a': Expect method name after '.'. |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |