Subject: | Consider documenting other styles of employing Type::Params |
After switching to Type::Params, I initially followed the style of declaring $check prior to the line that declares the variables to be populated. Doing so means that the variable names are distanced from the line containing the sub keyword by a minimum of one additional line; potentially more. As such, I found that this increased the cognitive load of discerning the (effective) signature of a sub, at a glance. After some consideration, I came up with the following approach.
sub f {
my ($str, $num) = do { state $check = compile(Str, Int) }->(@_);
}
Overall, I think that this is more legible, especially in the case that the validation spec is complex and merits being split over several lines.
Later, I realised that the module could peacefully co-exist with the experimental signatures feature.
use 5.26.0; # signatures are too slow in earlier versions
use experimental 'signatures';
# ...
sub f ($str, $num) {
do { state $check = compile(Str, Int) }->(@_);
}
Admittedly, the second approach doesn't mesh with the use of slurpy, although it's still possible to use a plain (@) or (%) signature. However, I think it scores well in terms of legibility and makes default handling more pleasant, even though it is still necessary to use the Optional type.
sub f ($str, $num = 0) {
do { state $check = compile(Str, Optional[Int]) }->(@_);
}
While it's not especially important, I was wondering whether anyone else might benefit from the first approach - at least - being mentioned in the documentation.