Skip to content

Commit 791d31e

Browse files
committed
overloading args objects
1 parent 5bcb9c4 commit 791d31e

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

lib/Type/Params.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ These methods start and end with double underscores to reduce the chance
653653
that they'll conflict with the name of a named parameter, however they are
654654
considered part of the public, supported API.
655655
656+
The object overloads C<< @{} >> to call C<< __TO_ARRAYREF__ >>.
657+
656658
=head4 C<< named_to_list >> B<< ArrayRef|Bool >>
657659
658660
The C<named_to_list> option is ignored for signatures using positional

lib/Type/Params/Signature.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,13 @@ sub make_extra_methods {
12201220
my $code = $coderef->code;
12211221
local $@;
12221222
eval( $code ) or die( $@ );
1223+
1224+
Type::Tiny::_install_overloads(
1225+
$self->bless,
1226+
'bool' => sub { 1 },
1227+
'@{}' => '__TO_ARRAYREF__',
1228+
'fallback' => !!1,
1229+
);
12231230
}
12241231

12251232
sub return_wanted {

meta/changes.pret

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,4 +1553,8 @@
15531553
a dcs:Addition;
15541554
label "Type::Params now provides hooks for the event of wrapping a sub in a signature.";
15551555
];
1556+
item [
1557+
a dcs:Addition;
1558+
label "Type::Params args objects now provide __TO_LIST__, __TO_ARRAYREF__, and __TO_HASHREF__ methods.";
1559+
];
15561560
].

t/20-modules/Type-Params/v2-named-methods.t

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use Types::Common -all;
3030

3131
our @ARGS;
3232

33-
signature_for [ qw/ get_list get_arrayref get_hashref / ] => (
33+
signature_for [ qw/ get_list get_arrayref get_hashref be_arraylike / ] => (
3434
named => [
3535
foo => Int, { alias => 'fool' },
3636
bar => Optional[Int],
@@ -151,4 +151,21 @@ subtest '__TO_HASHREF__' => sub {
151151
);
152152
};
153153

154+
sub be_arraylike {
155+
my $args = shift;
156+
[ @{$args} ];
157+
}
158+
159+
subtest '@{}' => sub {
160+
is_deeply(
161+
be_arraylike( foo => 66, bar => 99 ),
162+
[ 66, 99 ],
163+
);
164+
165+
is_deeply(
166+
be_arraylike( fool => 66 ),
167+
[ 66, undef ],
168+
);
169+
};
170+
154171
done_testing;

0 commit comments

Comments
 (0)