Subject: | Feature request: ability to check types with one sub call |
Currently Function::Parameters calls type validation routines for each argument listed, eg per B::Deparse:
sub doit : method {
use warnings;
use strict;
BEGIN {
$^H{'Function::Parameters/config'} = 'HASH(0x7f8ae731dee8)';
}
Function::Parameters::_croak('Too few arguments for method doit (expected 3, got ' . @_ . ')') if @_ < 3;
Function::Parameters::_croak('Too many arguments for method doit (expected 3, got ' . @_ . ')') if @_ > 3;
my $self = shift();
my($times, $base_str) = @_;
Function::Parameters::_croak('In method doit: parameter 1 ($times): ' . (\'Int')->get_message($times)) unless (\'Int')->check($times);
Function::Parameters::_croak('In method doit: parameter 2 ($base_str): ' . (\'Str')->get_message($base_str)) unless (\'Str')->check($base_str);
return $base_str x $times;
}
It'd be nice to have a way for a type checking library to specify a string which can be eval'd in to code at compile time, to avoid the overhead of calling the check sub on each argument. In other words, to generate code more like this:
sub doit : method {
use warnings;
use strict;
BEGIN {
$^H{'Function::Parameters/config'} = 'HASH(0x7f8ae731dee8)';
}
Function::Parameters::_croak('Too few arguments for method doit (expected 3, got ' . @_ . ')') if @_ < 3;
Function::Parameters::_croak('Too many arguments for method doit (expected 3, got ' . @_ . ')') if @_ > 3;
my $self = shift();
my($times, $base_str) = @_;
Function::Parameters::_croak('In method doit: parameter 1 ($times): ' . (\'Int')->get_message($times)) unless (($_=$times)//1) && defined && /^-?\d+$/;
Function::Parameters::_croak('In method doit: parameter 2 ($base_str): ' . (\'Str')->get_message($base_str)) unless (($_=$base_str)//1) && defined && !ref;
return $base_str x $times;
}
Maybe ask the type-checking class to implement a check_str() routine for this?