develooper Front page | perl.perl5.porters | Postings from September 2008

Re: unless(...) terser than if(!...)

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
September 7, 2008 14:59
Subject:
Re: unless(...) terser than if(!...)
Message ID:
20080907215930.GD75181@plum.flirble.org
On Thu, Aug 28, 2008 at 05:56:02PM -0500, Graham Barr wrote:

> >Couldn't we optimise the former to the latter fairly easily?
> 
> But they can be subtly different in the presence of overload
> 
> {
>   package foo;
>   use overload "!" => sub { 1 }, "bool" => sub { 2 }, fallback => 1;
> }
> 
> $a = bless [],'foo';
> 
> print "a\n" if $a;
> print "b\n" if !$a;
> print "c\n" unless $a;
> print "d\n" unless !$a;

True. And not something that I'd thought of. Which strikes me as a reason not
to make such a change in a maintenance branch, because it could conceivably
break working code.

But I feel no such qualms about a major release - using overloading to make
your value both true and false strikes me as crazy. Really anything that
deliberately has implementation inconsistencies between semantically equivalent
actions (such as $a = $a + 1; $a += 1; ++$a;) is in my mind bad practice and
foolhardy to rely on in perpetuity.

Nicholas Clark

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