We have:
print STDOUT : $a;
as indirect object syntax. The colon means "STDOUT is the object we're
operating on." It works everywhere. We also have
for 1..10 : 2 {...}
in which the colon indicates a step operation. The above will iterate
through the values 2,4,6,8,10.
My question is, how do you you know when : means step and not indirect
object?
For example, I would presume
for @a : 2 {...}
means step through @a by twos. But I would expect
foo @a : 2 {...}
to mean indirect object, calling @a.foo(2,{...})
So how's it know?
MikeL
Presumably you forgot the $ on that STDOUT.
# as indirect object syntax. The colon means "STDOUT is the
# object we're
# operating on." It works everywhere. We also have
#
# for 1..10 : 2 {...}
#
# in which the colon indicates a step operation. The above
# will iterate
# through the values 2,4,6,8,10.
#
# My question is, how do you you know when : means step and not
# indirect
# object?
#
# For example, I would presume
#
# for @a : 2 {...}
#
# means step through @a by twos. But I would expect
No. If you want to step by twos, you do this:
for @a -> $x, $y { ... }
# foo @a : 2 {...}
#
# to mean indirect object, calling @a.foo(2,{...})
#
# So how's it know?
I suspect that the prototype for '..' is like this:
sub infix:.. ($left: $right: $step //= 1) { ... }
So code like this:
1 .. 10 : 2
Effectively translates to this:
infix:..(1: 10: 2)
(i.e. the operator turns into a colon.) Thus, you disambiguate the same
way you normally do: with parentheses.
foo(1..10 : 2) #Presumably wrong
foo((1..10) : 2) #Presumably right
--Brent Dax <bren...@cpan.org>
@roles=map {"Parrot $_"} qw(embedding regexen Configure)
>How do you "test" this 'God' to "prove" it is who it says it is?
"If you're God, you know exactly what it would take to convince me. Do
that."
--Marc Fleury on alt.atheism
So the 'step' use of colon may _only_ be used in conjunction with a
"ranged" list, e.g. C<..>, correct? In _any_ other context, it means
something else.
In looking at A3, I also can't seem to find anything definitive on the
allowed operands to C<..>: specifically, if they can be anything but
literals, or integers.
Would all of the following therefore be syntax errors?
@a : 2
1 .. $a
1 .. $a : 2
$a .. $b
$a .. $b : 2
$a .. $b : $c
1 .. 10 : $c
2.5 .. 10.0 : 0.5
MikeL
In *all* contexts, it's a supercomma. C<..> interprets whatever comes
after the supercomma as being a step.
# In looking at A3, I also can't seem to find anything
# definitive on the
# allowed operands to C<..>: specifically, if they can be anything but
# literals, or integers.
They can be variables in Perl 5, so I suspect Perl 6 is fine with it
too.
# Would all of the following therefore be syntax errors?
#
# @a : 2
This isn't a syntax error, but it doesn't do what you want.
# 1 .. $a
# 1 .. $a : 2
# $a .. $b
# $a .. $b : 2
# $a .. $b : $c
# 1 .. 10 : $c
# 2.5 .. 10.0 : 0.5
To my knowledge, these are all fine.
Thanks, you're right. I was confusing the 'lazy' discussion with the
'range' discussion. All of those should work. As should
$a .. Inf
but not
Inf .. $a
:-)
MikeL