I can bind containers to new names:
my %h;
my $p := %h{ "a" }{ "b" }{ "c" };
$p = 1;
say %h;
Which outputs expected:
{a => {b => {c => 1}}}
But what if I need to return such pointer from subroutine?
my %h;
sub get-pointer {
my $p := %h{ "a" }{ "b" }{ "c" };
return $p;
};
my $q := get-pointer();
$q = 1;
say %h;
Gives:
Cannot assign to a readonly variable or a value
That thing puzzles me - $p.WHERE
and $q.WHERE
give the same address, so why is it suddenly read-only?
C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.
Yes to both. Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied.
If you declare a formal parameter of a function as a pointer type, you are passing that parameter by its address. The pointer is copied, but not the data it points to. So, Pass By Address offers another method of allowing us to change the original argument of a function (like with Pass By Reference).
Just like any other argument, pointers can also be passed to a function as an argument.
Nevermind, I had some tunnel-vision moment and wanted aliases to behave as C pointers.
Found it clearly explained here at Raku Documentation.
The sub return will return values, not containers. Those are immutable
To return a mutable container, use return-rw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With