Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: Distinguish between missing and undefined arguments with subroutine signatures (slurpy parameter)

by LanX (Saint)
on Dec 27, 2020 at 21:04 UTC ( [id://11125828]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)
in thread Distinguish between missing and undefined arguments with subroutine signatures

> To my understanding it is needed for an optional value argument in a signature.

Ah ... I see. Using a slurpy parameter @settings will solve this and fits much better into your use-case.

from perlsub

After positional parameters, additional arguments may be captured in a slurpy parameter. The simplest form of this is just an array variable:
sub foo ($filter, @inputs) { print $filter->($_) foreach @inputs; }

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^5: Distinguish between missing and undefined arguments with subroutine signatures (slurpy parameter)
  • Select or Download Code

Replies are listed 'Best First'.
Re^6: Distinguish between missing and undefined arguments with subroutine signatures (semipredicate problem)
by jo37 (Deacon) on Dec 27, 2020 at 21:13 UTC
    Using a slurpy parameter @settings will solve this and fits much better into your use-case.

    Sadly, it does not. I already considered this. It does not prevent the sub from being called with more arguments than allowed.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      > It does not prevent the sub from being called with more arguments than allowed.

      hmm ... you are going to extremes now. :)

      so

      ->mutate(FIELD) is a getter for FIELD

      ->mutate(FIELD,VALUE) is a setter for FIELD

      ->mutate(FILED,VALUE,OTHER...) shouldn't compile

      I think, in this particular case you can still apply a prototype of ($;$) together with the signature ($field,@values)

      At least theoretically, not sure if it's still implemented that way.

      UPDATE

      well not with methods, see other post:

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      > It does not prevent the sub from being called with more arguments than allowed.

      Wait a second....

      If you are talking about compile-time checking a ->method I'm pretty sure that this can't be done in dynamically typed languages like Perl.

      Like with prototypes I expect this only to be possible with normal subs. ( without static typing of the $obj it's impossible to tell which ->methods are allowed, normal subs OTOH can be pre-declared.)

      And when doing a dynamic check at run-time, using a @slurpy array is just good enough.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        If you are talking about compile-time checking ...

        No, I'm not talking about compile-time checking. I want method call checking at runtime. My sig_lexical example does what I want, but I'm not sure if this is a legal usage.

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-23 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found