Skip to content

Commit 29cd621

Browse files
committed
Optional and Optional[Any] treated the same by Type::Params
1 parent aaa6174 commit 29cd621

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

lib/Type/Params/Parameter.pm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ sub optional {
8181
: do {
8282
$_[0]{optional} = $_[0]->has_default || grep(
8383
$_->{uniq} == Optional->{uniq},
84-
$_[0]->type->parents,
84+
$_[0]->type, $_[0]->type->parents,
8585
);
8686
}
8787
}
@@ -173,6 +173,10 @@ sub _make_code {
173173
&& $constraint->parent
174174
&& $constraint->parent->{uniq} eq Optional->{uniq}
175175
&& $constraint->type_parameter;
176+
177+
# Allow Optional itself, without any parameter.
178+
$really_optional = Types::Standard::Any
179+
if $constraint && $constraint->{uniq} eq Optional->{uniq};
176180

177181
my $strictness;
178182
if ( $self->has_strictness ) {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,39 @@ is $o->myfunc2( arr => \@arr, int => 4 ), 'e', 'myfunc2 (happy path)';
9090
like $e, qr/Wrong number of parameters/, 'myfunc2 (param count exception)'
9191
}
9292

93+
94+
BEGIN {
95+
package Local::MyPackage2;
96+
97+
use strict;
98+
use warnings;
99+
100+
use Types::Standard -types;
101+
use Type::Params -sigs;
102+
103+
signature_for test => (
104+
method => !!1,
105+
named => [ foo => Optional, bar => Optional[Any] ],
106+
);
107+
108+
sub test {
109+
my ( $self, $arg ) = @_;
110+
my $sum;
111+
$sum += $arg->foo if $arg->has_foo;
112+
$sum += $arg->bar if $arg->has_bar;
113+
return $sum;
114+
}
115+
}
116+
117+
subtest 'Optional and Optional[Any] treated the same' => sub {
118+
my $o = bless {}, 'Local::MyPackage2';
119+
my $e = exception {
120+
is $o->test( foo => 2, bar => 5 ), 7;
121+
is $o->test( bar => 5 ), 5;
122+
is $o->test( foo => 2 ), 2;
123+
is $o->test( ), undef;
124+
};
125+
is $e, undef, 'No exception';
126+
};
127+
93128
done_testing;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,39 @@ my @arr = ( 'a' .. 'z' );
110110
like $e, qr/Wrong number of parameters/, 'myfunc2 (param count exception)'
111111
}
112112

113+
BEGIN {
114+
package Local::MyPackage2;
115+
116+
use strict;
117+
use warnings;
118+
119+
use experimental 'signatures';
120+
121+
use Types::Standard -types;
122+
use Type::Params -sigs;
123+
124+
signature_for test => (
125+
method => !!1,
126+
named => [ foo => Optional, bar => Optional[Any] ],
127+
);
128+
129+
sub test ( $self, $arg ) {
130+
my $sum;
131+
$sum += $arg->foo if $arg->has_foo;
132+
$sum += $arg->bar if $arg->has_bar;
133+
return $sum;
134+
}
135+
}
136+
137+
subtest 'Optional and Optional[Any] treated the same' => sub {
138+
my $o = bless {}, 'Local::MyPackage2';
139+
my $e = exception {
140+
is $o->test( foo => 2, bar => 5 ), 7;
141+
is $o->test( bar => 5 ), 5;
142+
is $o->test( foo => 2 ), 2;
143+
is $o->test( ), undef;
144+
};
145+
is $e, undef, 'No exception';
146+
};
147+
113148
done_testing;

0 commit comments

Comments
 (0)