-
-
Notifications
You must be signed in to change notification settings - Fork 48
Type::Params: unexpected error when die'ing in goto_next in a multiple signature #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
BTW, this is rather low priority, as there's a simple (rather less convoluted) workaround:
|
If the old API is this: foo( $hashref, %options ) Then this part seems wrong: {
# legacy
goto_next => \&goto_next,
head => [Str], # <-- Why `Str`? Shouldn't it be `HashRef`?
named => [ uri => Optional [Any], head => Optional [Any] ],
}, If changed to # This works
package ABC;
use strict;
use warnings;
use Data::Dumper;
use Types::Common qw( signature_for signature -types );
signature_for foo => (
multiple => [
{
# legacy
goto_next => sub { $_[1]{head} = $_[0]; return $_[1]; },
head => [ HashRef ],
named => [ uri => Optional[Any], head => Optional[Any] ],
},
{
# new
named => [ head => HashRef, uri => Optional[Any] ]
},
],
);
sub foo {
print Dumper @_;
}
package main;
ABC::foo( { xyz => 1 }, uri => 'about:blank' );
ABC::foo( head => { xyz => 1 }, uri => 'about:blank' ); |
In the next version, you'll be able to do: package ABC;
use strict;
use warnings;
use experimental qw( builtin signatures );
use Data::Dumper;
use Types::Common qw( signature_for -types );
signature_for get_page => (
named => [
headers => ArrayRef,
uri => Str,
username => Optional[Str],
password => Optional[Str],
],
list_to_named => builtin::true,
);
sub get_page ( $args ) {
print Dumper( $args );
}
package main;
ABC::get_page( headers => [ 'User-Agent' => 'Mozilla/1.0' ], uri => 'about:blank', username => 'bob', password => 'p4ssw0rd' );
ABC::get_page( [ 'User-Agent' => 'Mozilla/1.0' ], uri => 'about:blank', username => 'bob', password => 'p4ssw0rd' );
ABC::get_page( [ 'User-Agent' => 'Mozilla/1.0' ], 'about:blank', username => 'bob', password => 'p4ssw0rd' );
ABC::get_page( [ 'User-Agent' => 'Mozilla/1.0' ], 'about:blank' );
ABC::get_page( 'about:blank', [ 'User-Agent' => 'Mozilla/1.0' ] );
ABC::get_page( 'about:blank', [ 'User-Agent' => 'Mozilla/1.0' ], { username => 'bob', password => 'p4ssw0rd' } ); And they should all "just work". Basically when extracting named arguments from (Only for required parameters, though you can opt in to using the feature for optional parameters on a per-parameter basis.) |
Type::Tiny 2.004000
I'm trying to handle a legacy API issue by using multiple signatures. The old API is
i.e., first is positional, rest are named.
The new API is
i.e., all are named.
In order to transparently convert from the first to the second, I'm doing this:
I need to provide the
head
parameter to the named options for the legacy API otherwise the constructed parameter class doesn't have ahead
attribute.This however leaves open the possibility that this call:
could arise if someone tweaked
%options
to includehead
and forgot to fix the actual call.So I figured I could catch this via:
But this call
results in a rather unexpected error:
While debugging, I reverted to a similar approach with a single signature, i.e.
With the result that the call
results in the expected outcome:
The text was updated successfully, but these errors were encountered: