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

Re: Things you can't do in a signatured sub

Thread Previous | Thread Next
Darren Duncan
January 21, 2022 20:27
Re: Things you can't do in a signatured sub
Message ID:
On 2022-01-21 8:49 a.m., Ovid via perl5-porters wrote:
> On Wednesday, 19 January 2022, 19:00:12 CET, Paul "LeoNerd" Evans <> wrote:
>>        sub signatured($x = undef) {
>>          # impossible to distinguish signatured() from signatured(undef)
>>        }
> I knew my 3VL discussion went over like a lead balloon, but I couldn't resist being cheeky ...
>      use Unknown::Values;
>      sub foo ($bar = unknown) {
>          say is_unknown $bar ? "unknown" : "known";
>      }
>      foo();
> That prints "unknown", so if someone passed in `undef`, it will print "known" because someone explicitly assigned to it.

Again I repeat, it is VERY easy with normal signatures to distinguish missing 
arguments from explicitly undef arguments:

     sub signatured(@maybe_x) {
         if (scalar @maybe_x > 0) {
             # we were given an argument, which may or may not be undef
         else {
             # we were not given an argument

Also your Unknown feature is not the panacea you seen think it is.

How would your second example distinguish someone explicitly passing the unknown 
value as an argument from someone who didn't pass an argument?

The answer is that it can't, so Unknown adds nothing as far as a solution to the 
problem, your example is exactly the same as the original but more complicated.

In principle checks against a particular default value never work universally.

The ONLY solution that works universally is distinguishing that a collection 
element exists or doesn't exist.  What Perl exists() gives us on a hash, or 
arity checks on a collection etc.

So knowing if an argument was passed only works with the method I provided.

-- Darren Duncan

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