Inner classes: Difference between revisions

m
→‎{{header|Raku}}: Add a Raku example
(→‎{{header|ALGOL 68}}: Hide the print methods in the constructors)
m (→‎{{header|Raku}}: Add a Raku example)
Line 164:
vec[0] = 1
</pre>
 
=={{header|Raku}}==
Raku supports nested classes, however they are really only useful for grouping units of behavior together into a namespace. There is no particular benefit as far as access to private methods or attributes that can't be achieved by non-nested classes. Access to inner classes must use fully qualified paths.
 
<syntaxhighlight lang="raku" line>class Outer {
has $.value is rw;
method double { self.value ×= 2 }
 
class Inner {
has $.value is rw;
}
}
 
# New Outer instance with a value of 42.
my $outer = Outer.new(:value(42));
 
say .^name, ' ', .value given $outer;
$outer.double;
say .^name, ' ', .value given $outer;
 
# New Inner instance with no value set. Note need to specify fully qualified name.
my $inner = Outer::Inner.new;
 
# Set a value after the fact.
$inner.value = 16;
 
# There is no way for the Inner object to call the Outer .double method.
# It is a separate, distinct class, just in a funny namespace.
say .^name, ' ', .value given $inner;
$inner.value /=2;
say .^name, ' ', .value given $inner;</syntaxhighlight>
{{out}}
<pre>Outer 42
Outer 84
Outer::Inner 16
Outer::Inner 8</pre>
 
=={{header|Wren}}==
10,327

edits