develooper Front page | perl.perl5.porters | Postings from March 2022

Re: Pre-RFC: builtin:: functions for detecting numbers vs strings

Thread Previous | Thread Next
Darren Duncan
March 11, 2022 03:07
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
On 2022-03-10 6:36 p.m., demerphq wrote:
> Agreed.
> Fwiw The opposite outcome, forcing a value to be a number internally would be:
> $num = 0+$num;
> Although saying that I wonder what we do these days with
> $num += 0;
> I haven't checked but I hope the two produce the same outcome.

I have zero doubt those produce the same outcome.  Any "foo=" operators are just 
shorthands for assigning the result of "foo", and so the type of $num must be a 
number because "+" is the operation.

BUT, strictly speaking, the equivalence is ACTUALLY these 2:

   $num = $num + 0;

   $num += 0;

It gives the same result the other way only because + is commutative.

Theoretically, if we had a non-commutative "foo" operator such that foo(x,y) and 
foo(y,x) returned a result of a different type depending on the arguments, then 
what exactly you expand the "foo=" to for argument order is important.

But as long as we keep the correct order in mind, absolutely they should be the 
same outcome.

-- Darren Duncan

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About