single element lists

1 view
Skip to first unread message

Jonathan Scott Duff

unread,
May 11, 2005, 12:45:12 PM5/11/05
to perl6-l...@perl.org

We're discussing the proper semantics of (1)[0] on #perl6. Here's
where we're at so far:

1. specialise ()[] to parse as (,)[]
2. scalars are singleton lists, so ()[] naturally
3. make (1)[0] die horribly.

We all seem to agree that #3 is least useful and probably wrong. But
there's a divide between whether #1 or #2 is the "right" behavior.

#2 implies that (1)[0][0][0][0] == 1
#1 means that (1)[0] == 1 and (1)[0][0] is an error

FWIW, I'm in favor of #1

What does p6l think? (What does @Larry think?)

-Scott
--
Jonathan Scott Duff
du...@pobox.com

Juerd

unread,
May 11, 2005, 12:52:31 PM5/11/05
to perl6-l...@perl.org
Jonathan Scott Duff skribis 2005-05-11 11:45 (-0500):

> 1. specialise ()[] to parse as (,)[]
> 2. scalars are singleton lists, so ()[] naturally
> 3. make (1)[0] die horribly.
> #2 implies that (1)[0][0][0][0] == 1
> #1 means that (1)[0] == 1 and (1)[0][0] is an error

#1 also means that ($aref)[0] is $aref, rather than $aref[0].

I pick #2, also because I think being able to pass scalars where arrays
are expected simplifies other parts of the language as well.


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Thomas Sandlaß

unread,
May 11, 2005, 12:50:30 PM5/11/05
to perl6-l...@perl.org
Jonathan Scott Duff wrote:
> What does p6l think? (What does @Larry think?)

I favor #3 as syntax error.
But note $TSa == all( none(@Larry), one($p6l) ) or so :)
--
TSa (Thomas Sandlaß)

Aaron Sherman

unread,
May 11, 2005, 1:36:17 PM5/11/05
to du...@pobox.com, Perl6 Language List
On Wed, 2005-05-11 at 12:45, Jonathan Scott Duff wrote:
> We're discussing the proper semantics of (1)[0] on #perl6. Here's
> where we're at so far:
>
> 1. specialise ()[] to parse as (,)[]
> 2. scalars are singleton lists, so ()[] naturally
> 3. make (1)[0] die horribly.

It may or may not help, but I direct your attention to:

http://groups-beta.google.com/group/perl.perl6.language/browse_frm/thread/24ef8f421548b806/f119fc38427f9f3b?q=comma+one+element&rnum=2#f119fc38427f9f3b


--
Aaron Sherman <a...@ajs.com>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback


Craig DeForest

unread,
May 11, 2005, 1:32:19 PM5/11/05
to du...@pobox.com, perl6-l...@perl.org
My perspective from PDL is that "(1)[0][0][0]"..."[0]" should evaluate
to 1. The artificial distinction between a scalar and an array of
length 1 (in each dimension) is the source of endless hassles, and it's
a pretty simple DWIM to allow indexing of element 0 of any unused
dimension. That makes it much, much easier to write code that
generalizes, because you don't have to check for the scalar case -- you
can always assume that any given dimensional axis will always have one
value along it (*), and loop/thread/whatever along that dimension.

(*) Of course, that's assuming that there's at least one value in the
list as a whole -- PDL does, and perl 6 should, support zero-element
lists; any looping or threading construct on a zero-element list is a no-op.

Rob Kinyon

unread,
May 11, 2005, 1:18:01 PM5/11/05
to Juerd, perl6-l...@perl.org
On 5/11/05, Juerd <ju...@convolution.nl> wrote:
> Jonathan Scott Duff skribis 2005-05-11 11:45 (-0500):
> > 1. specialise ()[] to parse as (,)[]
> > 2. scalars are singleton lists, so ()[] naturally
> > 3. make (1)[0] die horribly.
> > #2 implies that (1)[0][0][0][0] == 1
> > #1 means that (1)[0] == 1 and (1)[0][0] is an error
>
> #1 also means that ($aref)[0] is $aref, rather than $aref[0].
>
> I pick #2, also because I think being able to pass scalars where arrays
> are expected simplifies other parts of the language as well.

#2 is also more intuitive and comfortable. () in P5 constructs a list,
which is why (split ' ', $foo)[0] works so nicely.

Autrijus Tang

unread,
May 11, 2005, 4:19:02 PM5/11/05
to perl6-l...@perl.org
On Wed, May 11, 2005 at 01:11:45PM -0700, Larry Wall wrote:
> On Wed, May 11, 2005 at 11:45:12AM -0500, Jonathan Scott Duff wrote:
> :
> : We're discussing the proper semantics of (1)[0] on #perl6. Here's

> : where we're at so far:
> :
> : 1. specialise ()[] to parse as (,)[]
> : 2. scalars are singleton lists, so ()[] naturally
> : 3. make (1)[0] die horribly.
> :
> I think it has to be #1. We can't have (@foo)[0] working at two different
> indirection levels depending on whether @foo == 1.

Hm? Under #2, no matter whether @foo is (1) or (1,2), the construct
(@foo)[0] would always means @foo.[0]. Not sure how the length of @foo
matters here.

Thanks,
/Autrijus/

Larry Wall

unread,
May 11, 2005, 4:11:45 PM5/11/05
to perl6-l...@perl.org
On Wed, May 11, 2005 at 11:45:12AM -0500, Jonathan Scott Duff wrote:
:
: We're discussing the proper semantics of (1)[0] on #perl6. Here's

I think it has to be #1. We can't have (@foo)[0] working at two different


indirection levels depending on whether @foo == 1.

Larry

rottcodd

unread,
May 11, 2005, 5:22:55 PM5/11/05
to
What if $f is an object in a vector-like class that has an overloaded
postcircumfix[] ? Does ($f)[0] == $f or ($f)[0] == $f[0] ? What about
($f1 + $f2)[0] ? (Assuming the vector-like class also overloads +) If
($f1 + $f2)[0] == $f1 + $f2, how would you get the other meaning?

Larry Wall

unread,
May 11, 2005, 5:12:41 PM5/11/05
to perl6-l...@perl.org
On Thu, May 12, 2005 at 04:19:02AM +0800, Autrijus Tang wrote:
: Hm? Under #2, no matter whether @foo is (1) or (1,2), the construct

: (@foo)[0] would always means @foo.[0]. Not sure how the length of @foo
: matters here.

Tell you what, let's require P5's (...)[] to be translated to [...][],
so (...)[] should assume scalar context that will return some kind of
array reference. (What Luke said about (1,(2,3),4)[] still holds, though.
Commas create lists, and lists by default impose list context, and
parens are only for grouping in lists, not scalarifiying.)

Larry

Autrijus Tang

unread,
May 11, 2005, 5:19:11 PM5/11/05
to perl6-l...@perl.org

Sure (and done). Now that #1 is eliminated, the question is now
whether a simple scalar can be treated as a small (one-element) array
reference, much like a simple pair can be treated as a small
(one-element) hash reference.

1.[0]; # evaluates to 1?

If yes, then (1)[0] means the same as 1.[0] and 1.[0][0][0]. If no,
(1)[0] is a runtime error just like 1.[0] -- i.e. unable to find the
matching .[] multisub under Int or its superclasses.

Thanks,
/Autrijus/

Larry Wall

unread,
May 11, 2005, 6:07:41 PM5/11/05
to perl6-l...@perl.org
On Thu, May 12, 2005 at 05:19:11AM +0800, Autrijus Tang wrote:
: Sure (and done). Now that #1 is eliminated, the question is now

: whether a simple scalar can be treated as a small (one-element) array
: reference, much like a simple pair can be treated as a small
: (one-element) hash reference.
:
: 1.[0]; # evaluates to 1?
:
: If yes, then (1)[0] means the same as 1.[0] and 1.[0][0][0]. If no,
: (1)[0] is a runtime error just like 1.[0] -- i.e. unable to find the
: matching .[] multisub under Int or its superclasses.

Maybe we should just let someone poke a Subscriptable role into some
class or other to determine the behavior if they care.

Larry

Wolverian

unread,
May 11, 2005, 4:42:26 PM5/11/05
to perl6-l...@perl.org
On Wed, May 11, 2005 at 11:45:12AM -0500, Jonathan Scott Duff wrote:
> We're discussing the proper semantics of (1)[0] on #perl6. Here's
> where we're at so far:
>
> 1. specialise ()[] to parse as (,)[]
> 2. scalars are singleton lists, so ()[] naturally
> 3. make (1)[0] die horribly.

(1)[0] means 1[0], which is probably undefined, so it dies. That could
be detected at compile time. (my $foo = 1)[0] means $foo[0], which would
die at runtime, unless there's type inference going on. In any case, I
don't see a List in ()[] without a list-creating expression in the ().

> -Scott

--
wolverian

signature.asc

James Mastros

unread,
May 12, 2005, 5:52:38 PM5/12/05
to perl6-l...@perl.org
Larry Wall wrote:
> : If yes, then (1)[0] means the same as 1.[0] and 1.[0][0][0]. If no,
> : (1)[0] is a runtime error just like 1.[0] -- i.e. unable to find the
> : matching .[] multisub under Int or its superclasses.
>
> Maybe we should just let someone poke a Subscriptable role into some
> class or other to determine the behavior if they care.
Why is this a role, rather then just implementing postcircumfix:«[
]»(Whatever $self: Int $index) ? (I'd hope the error message is a bit
more newbie-friendly, but that's the only special-casing I see it
needing...)

-=- James Mastros

Larry Wall

unread,
May 13, 2005, 4:22:55 PM5/13/05
to perl6-l...@perl.org

Different abstraction level. Maybe defining the postcircumfix is
exactly what the role does.

Larry

Reply all
Reply to author
Forward
0 new messages