Perl6: variable number of arguments to function/subroutine

后端 未结 3 1682
情深已故
情深已故 2021-01-11 13:29

I want to be able to run a function with a variable number of parameters in Perl6, but after reading through https://docs.perl6.org/language/functions#Arguments I don\'t see

3条回答
  •  耶瑟儿~
    2021-01-11 13:58

    If you just want to be able to take 1 or 2 values then the easiest way is to mark the second value as optional :

    sub some-test( $v1, $v2? ) { ... }
    

    Or you can define a default value :

    sub some-test( $v1, $v2="default" ) { ... }
    

    Or if you want any number of values (1 or more) you can use a slurpy with a where clause :

    sub some-test( *@v where *.elems > 0 ) { ... }
    

提交回复
热议问题