Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array/Colon question

3 views
Skip to first unread message

Michael Lazzaro

unread,
Jan 23, 2003, 1:13:41 PM1/23/03
to perl6-l...@perl.org

Here's something that I'm still confused about.

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

Brent Dax

unread,
Jan 23, 2003, 5:24:51 PM1/23/03
to Michael Lazzaro, perl6-l...@perl.org
Michael Lazzaro:
# Here's something that I'm still confused about.
#
# We have:
#
# print STDOUT : $a;

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

Michael Lazzaro

unread,
Jan 24, 2003, 12:42:51 PM1/24/03
to Brent Dax, perl6-l...@perl.org

On Thursday, January 23, 2003, at 02:24 PM, Brent Dax wrote:
> I suspect that the prototype for '..' is like this:

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

Brent Dax

unread,
Jan 24, 2003, 1:10:23 PM1/24/03
to Michael Lazzaro, perl6-l...@perl.org
Michael Lazzaro:
# On Thursday, January 23, 2003, at 02:24 PM, Brent Dax wrote:
# > I suspect that the prototype for '..' is like this:
#
# 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 *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.

Michael Lazzaro

unread,
Jan 24, 2003, 2:39:17 PM1/24/03
to Brent Dax, perl6-l...@perl.org

On Friday, January 24, 2003, at 10:10 AM, Brent Dax wrote:
> # 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

0 new messages