Open tobyink opened 3 years ago
From perl@toby.ink on 2014-11-06 08:26:18 :
The behaviour of the constructor is correct. Not quite sure why the function is allowing undef here. The difference seems to depend on whether named parameters are passed as a flattened hash or a hashref:
cPickyConstructor::okFunction( x => "", y => undef ); # lives
cPickyConstructor::okFunction({ x => "", y => undef }); # dies
Migrated from rt.cpan.org #100061 (status was 'open')
Requestors:
Attachments:
From sven.schober@uni-ulm.de on 2014-11-05 15:09:38 :
Hi!
I stumbled across the following today and just wanted to check, if this is expected behaviour:
!/usr/bin/env perl
use Modern::Perl '2014'; use Moops; use Test::More; use Test::Exception;
class cPickyConstructor { method BUILD ( Str :$x, Str :$y? ){ } fun okFunction ( Str :$x, Str :$y? ){ } };
class cNotSoPickyConstructor { method BUILD ( Str :$x, Str|Undef :$y? ){ } };
dies_ok( sub { cPickyConstructor->new( x => "", y => undef) }, 'Constructor complains about
undef
not satisfyingStr
' );lives_ok( sub{ cPickyConstructor::okFunction( x => "", y => undef) }, 'But function does not care' );
lives_ok( sub { cNotSoPickyConstructor->new( x => "", y => undef) }, 'Need to explicitly specify
Undef
as type constraint in constructor' );done_testing();
Cheers, Sven