I have this code with two functions, the second function has some named parameters with default values:
sub func1($x,$y) {
# do something
}
sub some_func($x, $y , :$vel = 1, :$acceleration = 1/$vel) {
# if $vel and $acceleration both are not missing
# call func1($x, $y)
# else do something else
}
As we know the second function can be called in following ways:
some_func($x, $y); #vel and acceleration are missing
some_func($x, $y, vel => 2, acceleration => 5); #vel and acceleration are not missing
In second call, named parameters: vel and acceleration are passed. How can I check within the function body that these
two named parameters are being passed?
Readings:
Keeping default values for nested named parameters
https://docs.raku.org/type/Signature
Why does constraining a Perl 6 named parameter to a definite value make it a required value?
There are multiple ways to achieve this, though – as far as I know – all of them involve changing
&some-func's definition (not just its body).I'd probably do it like this: split the function into a
multiwith one candidate that handles the case where both arguments are passed. You can do that by marking the named arguments asrequiredfor that candidate a!. Here's how that would look:Or you could do it by dropping the default values from the function signature and then setting them in the body (this solution will likely look familiar to anyone used to a programming language that doesn't support default values). Note that modifying
$veland$accelerationrequires marking them asis copy.Another approach would be to capture all of the arguments to
some-funcbefore assigning defaults and then to inspect theCapture. That might look like:(Note that this approach gives slightly worse error messages when
some-funcis passed the wrong arguments)Or you could give each default value a marker
rolethat you can test against in the function body:Or, a slight variant of the above: use a string literal to auto-generate a role rather than defining one manually. This is slightly less typesafe – it doesn't protect against typos in the role name – but is also a bit more concise.
Like I said, I'd go with the
multi. But I hope that seeing several different ways helps you think about the problem – and maybe even teaches a bit of more advanced Raku syntax.