develooper Front page | perl.perl5.porters | Postings from November 2010

Re: [PATCH] Add "safe arrow/safe dereference" operator: &&->

Thread Previous | Thread Next
From:
David Caldwell
Date:
November 13, 2010 10:21
Subject:
Re: [PATCH] Add "safe arrow/safe dereference" operator: &&->
Message ID:
B5203CBA5420A9204068C51F@black.porkrind.org
On 11/13/10 1:33 PM +0100 demerphq wrote:

> On 13 November 2010 05:56, David Caldwell <david@porkrind.org> wrote:
>> Hello,
>>
>>  This patch adds support for a new operator "&&->" that acts like arrow
>> but evaluates to undef if the left side is undef.
>>
>> Example:
>>  $complicated&&->complex&&->{big}&&->{long}&&->[0]&&->{nested}&&->[1]&&
>> ->() Instead of:
>>  $complicated &&
>>  $complicated->complex &&
>>  $complicated->complex->{big} &&
>>  $complicated->complex->{big}->{long} &&
>>  $complicated->complex->{big}->{long}->[0] &&
>>  $complicated->complex->{big}->{long}->[0]->{nested} &&
>>  $complicated->complex->{big}->{long}->[0]->{nested}->[1] &&
>>  $complicated->complex->{big}->{long}->[0]->{nested}->[1]->()
>
> I dont understand really why this needs new syntax. Why cant it be
> done with a pragma.
>
> no autovivification 'lookup';
>
> something like that...

The no autovivification part is only half the patch. The other half is the 
method and subroutine "maybe" calls. The patch was born of me having to 
write a big long "&&" chain similar to the example I posted and getting 
annoyed at how much duplication there was.

I personally don't like the pragma technique because it would need to be 
turned on and off frequently which is unwieldy (someone already posted a 
good example of that). Along those lines, the new syntax allows for:

    $x &&-> y -> x &&-> q;

for the times when you know y() returning undef is fatal but $x and x() 
being/returning undef is not. That would be nigh impossible with a pragma.

> Er, forgive my manners twofold please. First for top posting. Sorry.

I fixed it for you :-). Forgive me replying to both of your emails at once.

> I just wonder if such syntax really is the way forward.
>
> On one hand it makes a duck and a goose look different. Thats good.
>
> But on the other it opens a serious can of worms...
>
> Such as explaining why we have two dereference operators,

Having 2 dereference operators has precedent. Perl6, as I noted before, has 
the "&." operator (although I suppose you could look at it as just the 
normal "&" and "." operators combined with the crazy short-circuiting 
behavior of perl6). I was just reading about CoffeeScript and noticed they 
had a similar "existential operator" accessor "?." 
(<http://jashkenas.github.com/coffee-script/#existence>). Groovy has a 
similar operator "?." 
(<http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29>).

> and the host of inevitable bugs that will come out of the woodwork once
> this is released. All the weird edge cases nobody thought about.....

I understand. I think the utility of the operator trumps the bugs that will 
occur. I, of course, say that as someone who writes a lot of perl code but 
doesn't have the responsibility of maintaining the perl source itself :-).

-David


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