develooper Front page | perl.perl5.porters | Postings from December 2021

Re: Pre-RFC: `unknown` versus `undef`

Thread Previous | Thread Next
From:
Darren Duncan
Date:
December 19, 2021 10:48
Subject:
Re: Pre-RFC: `unknown` versus `undef`
Message ID:
f8feb1a2-7e50-a8fa-4fd4-799d6196fb38@darrenduncan.net
On 2021-12-19 2:13 a.m., Martijn Lievaart wrote:
> Op 19-12-2021 om 00:18 schreef Paul "LeoNerd" Evans:
>> I think if you were to present this if/else code to anyone not
>> intimately familiar with this three-value logic idea, and tell them
>> there is a value you can put in $foo which makes neither branch
>> execute, they would look at you strangely and consider you quite mad.
>>
>> There may be a Lewis Carroll quote applicable here...
> 
> My thoughts exactly. But even worse, this will introduce bugs in existing code, 
> unless you need a feature flag for this to happen, in which case code without 
> the feature flag (for instance a library called from code that uses unknown) 
> running against this situation probably should die. Silently skipping both 
> branches of an if is insane if not guarded by a feature flag and only slightly 
> less insane with.
> 
> So what to do? Make such code die? But that reintroduces the original problem, 
> then we have a new singleton value which avoids many calls to defined(), just to 
> replace them with calls to is_unknown() (in other places, but still).
> 
> I do think there must be a satisfactory answer to this question before we can 
> proceed with implementation. It does not have to stand in the way of an RFC.

Well the other reasonable solution is to treat the pair as "if the expression 
result is true" and "if the expression result is anything else".  I actually 
thought of proposing that and then I didn't.

This also means a ternary expression does NOT result in unknown if the test 
expression is unknown, rather it returns the "else" expression.

However, doing that means these 2 things will behave differently in the presence 
of unknown where they wouldn't otherwise:

   if ($x) {
     foo();
   }
   else {
     bar();
   }

   if ($x) {
     foo();
   }
   if (!$x) {
     bar();
   }

Now that may be less surprising and more ideal in the presence of unknown.

What do you think of this alternative?

-- Darren Duncan

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