Hi,
Juerd wrote:
> Ingo Blechschmidt skribis 2005-11-20 16:44 (+0100):
>> Where is the difference (for the user) between a subroutine which
>> returns an appropriate proxy object and an array?
>
> The big difference between pure arrays and referenced arrays, for the
> user, is that pure arrays flatten in list context, while referenced
> arrays do not. Especially with for, this is very relevant.
I'd formulate this as "that @-arrays, i.e. variables whose sigil is @,
flatten in list context, while $-arrayrefs do not".
> I don't know if it is possible for an object to flatten in list
> context, but I would be surprised if it turned out to be.
For sure it is! Recall that "my @array = <a b c>" creates an object.
> Scalars should NEVER flatten in list context.
Yep. Again, I'd formulate this as "variables whose sigil is $ should
NEVER flatten in list context".
> This includes all references
Yep.
> and thus objects.
I disagree -- @array and %hash, created by plain old assignment ("my
@array = <a b c>", "my %hash = (...)"), are objects.
>> I think it'd even optimize many cases:
>> for @foo ¥ @bar {...}
>
> All for-optimizations are in the for, in Perl 5. It would be nice to
> have these in the actual functions and operators in Perl 6. Then
> for-reverse is not a special case anymore, and the optimization is
> indeed optimizing other cases too.
Yep. Also note that "for" is not a special magical construct in Perl 6,
it's a simple subroutine (&statement_control:<for>, with the signature
([EMAIL PROTECTED], Code *&code)). (Of course, it'll usually be optimized.)
Example:
{
my sub statement_control:<for> ([EMAIL PROTECTED], Code *&code) {
map &code, reverse @array;
}
for <a b c> -> $item { say $item }
# "c\nb\na\n"
}
# for restored, as the modified for went out of scope:
for <a b c> -> $item { say $item }
# "a\nb\nc\n"
>> (BTW, IIUC, per r6622 of S06.pod [1] &zip returns an array of
>> arrayrefs now:
>> for zip('a'...; 0...; @foo) -> [$a, $i, $x] { ...}
>
> Hm, that's sufficiently ugly. Is this really needed?
>
> I quite like how
>
> for @foos Y @bars -> $foo, $bar { ... }
>
> looks, and don't quite like
>
> for @foos Y @bars -> [ $foo, $bar ] { ... }
>
> as much.
I agree completely.
>> (Or does for no longer automatically take as much elements from the
>> input array as needed?
>
> I like the arity-sensitivity solution better, I think.
Me too.
--Ingo