tobyink / p5-moops

16 stars 4 forks source link

Inconsistent type constraint checking behaviour with optional, named parameter #17

Open tobyink opened 3 years ago

tobyink commented 3 years ago

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 satisfying Str' );

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

tobyink commented 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