develooper Front page | perl.perl5.porters | Postings from October 2013

Re: key/value hash slices status

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
October 27, 2013 20:49
Subject:
Re: key/value hash slices status
Message ID:
20131027204944.20489.qmail@lists-nntp.develooper.com
Ruslan Zakirov wrote:
> On Tue, Sep 17, 2013 at 2:22 AM, Ricardo Signes
> <perl.p5p@rjbs.manxome.org>wrote:
> 
> > * Father Chrysostomos <sprout@cpan.org> [2013-09-13T16:20:41]
> > > Should each %sliced{...} die?  The whole each $scalar feature is
> > > marked as experimental and is controversial.  I’m not sure I want to
> > > touch it, at least not until the pumpking says something.
> >
> >   my @a = ([9],[8],[7],[6],[5]);
> >
> >   while (my ($k,$v) = each @a[1,3]) {
> >     say "$k -> $v";
> >   }
> >
> > The results of that expression are comprehensible, but surprising.  If
> > that is
> > meant to work the way it does, I'm not sure I can justify %h{...} not
> > doing the
> > same.  That said, both make me uncomfortable.  (I imagine myself explaining
> > this to someone after a future bug report, and apologizing.)
> >
> > Alternately, why should they differ?
> >
> 
> This is really some very useless behavior.
> 
> `each` has a prototype and it's either array (container) or a scalar (ref
> to an array), it's more complex than that, but still enough. slices are
> neither, they are lists.
> 
> $ perl -E 'say each ([1], [2])'
> 02
> 
> 5.12 and older produce very nice error:
> Type of arg 1 to each must be hash or array (not list)
> 
> There are two ways to deal with it:
> 
> 1) die when lists or operators returning lists are used as argument
> 
> This is probably more painful to implement, but opens door to some useful
> results of such calls in the feature.
> 
> 2) help developer with a warning
> 
> What I'm proposing is "if it's not @a{1} better written as $a{1} and it's
> scalar context then throw 'Probably wrong use of slice in scalar context' ".

I think it is a good idea, since it would almost always be a mistake.
But I would suggest wording it as "%hash{...} in scalar context better
written as $hash{...}", regardless of whether the subscript looks
like a scalar.  (And, when possible, we would replace "..." with the
subscript.)

Does anybody object?


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About