Course of Raku / Advanced / Operators / Traits and pragmas 🆕 / Exercises / A writable parameter
Solution: A writable parameter
Here is a possible solution to the task.
Code
sub double($n is rw) {
$n *= 2;
}
my $score = 21;
double($score);
say $score;🦋 You can find the source code in the file writable-parameter.raku.
Output
42Comments
is rwbinds the parameter$nto the caller’s variable$score, so$n *= 2insidedoublechanges$scoreitself — which is why it prints42.Without
is rw, the parameter would be read-only and$n *= 2would be a compile-time error. Marking itis copyinstead would givedoublea private copy to modify, leaving$scoreuntouched at21.