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

Re: postfix dereference syntax

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
July 3, 2013 15:09
Subject:
Re: postfix dereference syntax
Message ID:
20130703150159.GD21195@fernweh.plasmasturm.org
* Dave Mitchell <davem@iabyn.com> [2013-07-03 16:25]:
> Alternatively, what about instead of expr->@*, we use
>
>     expr->[]?
>
> This is currently a syntax error. It has the mnemonic of
> a list slice that contains all elements. Ditto expr->{}.

The same occurred to me just after reading Kent’s mail.

> It's also currently a syntax error in quoted strings, so can easily be
> used there too:
>
>     print "$a->{}bar"

Beautiful!

It’s fine to say that quoting already has a lot of special cases, in the
long run I don’t think it’s something that could be gotten away with.
But I expected calls for support in interpolation to happen as soon as
people started using postfix deref, and if I was right on that, then it
was going to be added eventually, regardless of argument, so I chose to
make none, and leave this to play itself out.

But I did worry about the need to change the meaning of unlikely but
valid code when that happened.

That this syntax avoids this problem to start with is great.

Except…

> It could conceivably be expanded to indicate slicing, such as
>
>     $a->[1,5,7]
>
> *except* that this is currently valid syntax, where the list of
> indices is evaluated in scalar context, which discards all but the
> last element. So the above is currently equivalent to
>
>     $a->[7]
>
> Potentially we could deprecate the current behaviour, then (if it turns
> out not to be commonly used), repurpose it. This would also allow slices
> in strings, e.g. "$a->[1,5,7]", which also currently evaluates to
> "$a->[7]".

… here again I *am* worried. Because this isn’t the worst of it. If it
was just array slices, this wouldn’t be too bad. If push came to shove
it would be possible to say “we’re not doing slices (yet, at least)”,
and they *could* be written like this:

    ( $foo->bar->baz->[] )[1,5,7]

Obviously you might as well write that the old way:

    @{ $foo->bar->baz }[1,5,7]

The first even looks slightly less punctualicious (it omits the array
sigil and the parens are less noisy than the curlies).

So, not great, but it’s all liveable.


However… *this* runs into big trouble:

    $foo->bar->baz->{qw( foo bar baz )}

That’s not a hash slice… that already means a faux multidimensional
array. And if you try to punt?

    ( $foo->bar->baz->{} ){ # … sorry, Dave, I’m afraid I can’t do that

Woops. Ain’t no way but the old one:

    @{ $foo->bar->baz }{qw( foo bar baz )}


And then there is another puzzle: suppose that was solved somehow. (I
have an idea. But now is not the time.) Fine.

Then how to integrate Ruslan’s new hash slices with this syntax? It
doesn’t really have room to accommodate more than one type of slice.

And then it dawns on me that this problem extends further: what syntax
do you use to postfix-deref scalars in this proposal? (Or subs… or uh,
typeglobs, yeah I know.)

And now I sit here with a sinking feeling. I don’t think this is doable…

I’d *LOVE* if someone had a bright idea to address all that. I really
think this is the right syntax for the part of postfix-deref that can be
covered with this syntax.


Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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