This is all about a Scalar
that hasn't decontainerized.
You could change return $d
to return @$d
One option to get the same behavior is to alter the b
routine.
You have written "These subroutines both return an array, but the returned arrays behave differently.". But, as Holli notes, b
instead returns the Scalar
bound to $d
(that in turn contains an array):
sub b { my $d = [ 1, 2, 3, 4 ]; return $d };
say b().VAR.WHAT; # (Scalar)
You could change that by "decontainerizing" $d
, eg by prepending @
:
sub b { my $d = [ 1, 2, 3, 4 ]; return @$d };
say b().VAR.WHAT; # (Array)
my @f = b();
say @f; # [1 2 3 4]
say @f[1]; # 2
Or change @f = b()
to @f = b[]
If you don't decontainerize the value returned by b
, then a second issue / opportunity arises.
Regardless of what b
returns, the @f
assignment will evaluate the list of values being assigned to it. In a list context, Scalar
s are left as is (just as they were with a plain return $d
). So if you do not change b
to decontainerize, then you'll need to do so in the assignment to @f
instead, if you want @e
and @f
to end up the same.
This time you can't just prepend @
to do so. Because that would spell @b
-- which Raku would interpret as an @b
variable.
One option is to write @f = @(b())
, but that would be ugly / non-idiomatic. Another option is to write @f = b[]
. This takes advantage of the fact that the parens in the b
calls were redundant. Appending []
(a "zen slice") has the same effect as writing @(b)
, but is one character less.
So, to decontainerize in the list assignment, you could write:
sub b { my $d = [ 1, 2, 3, 4 ]; return $d };
say b().VAR.WHAT; # (Scalar)
my @f = b[];
say @f; # [1 2 3 4]
say @f[1]; # 2
"to make clear in the documentation"
What would be the correct expression to make clear in the documentation which array type what is returned by the subroutine?
I'm not sure what you mean by this question, even with the switch to merely "what is returned".
I'm also not sure what to point you to in the doc, and even if there is anywhere good to point you to, relative to the scenario in your SO.
I do know that, if it were me, I'd find the following doc sections confusing, relative to your scenario:
The Scalar containers and listy things section Holli linked. That section seems to me to currently be about use of Scalar
containers inside lists/arrays, which is relevant for the second issue I wrote about above ($d
is in a list on the rhs of the assignment to @f
). But that's not relevant to the first issue I wrote about (return $d
from the b
routine). There things are the other way around, namely there's an array inside a Scalar
.
The Scalar containers section earlier on the same page. The opening sentence -- "Although objects of type Scalar
are everywhere in Raku, you rarely see them directly as objects, because most operations decontainerize ..." works for me. But "a routine can return a container if it is marked as is rw
" is more problematic. It is true:
my $x = 23;
sub f() is rw { $x };
f() = 42;
say $x; # OUTPUT: «42»
But one doesn't have to mark a routine is rw
to return a container. One can use the return
routine, as you did:
my $x = 23;
sub f() { return $x };
say f().VAR.WHAT; # OUTPUT: «Scalar»