mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 10:01:38 +00:00
Perl6: Explain that Integers Can't be Passed and Modified in Sub's Even If Using `is rw´ (#2471)
* Explain that you cannot pass immutable values like integers to subs even if you use $n is rw. * Add myself as a contributor * Remove contributor since I am not a major contributor
This commit is contained in:
parent
c08ca5b9c9
commit
0d11887c22
@ -193,6 +193,15 @@ sub mutate($n is rw) {
|
||||
say "\$n is now $n !";
|
||||
}
|
||||
|
||||
my $m = 42;
|
||||
mutate $m; # $n is now 43 !
|
||||
|
||||
# This works because we are passing the container $m to mutate. If we try
|
||||
# to just pass a number instead of passing a variable it won't work because
|
||||
# there is no container being passed and integers are immutable by themselves:
|
||||
|
||||
mutate 42; # Parameter '$n' expected a writable container, but got Int value
|
||||
|
||||
# If what you want a copy instead, use `is copy`.
|
||||
|
||||
# A sub itself returns a container, which means it can be marked as rw:
|
||||
|
Loading…
Reference in New Issue
Block a user