Subject: | submethod BUILD with parameters and name conflict has strange behavior |
Hello again,
attached a failing test for BUILD with named parameters.
I am not sure about the correct behavior for Test5, but undef for $foo in BUILD is in my option not an option.
And Test6 should work as expected when using labeled parameters.
Subject: | build_with_parameters.t |
use warnings;
use strict;
use Dios;
use Test::More;
class Test1 {
has $.foo = 1;
submethod BUILD {
::is($foo, 1, 'Test1 - $foo from attribute after default BUILD for $foo');
}
}
class Test2 {
has $.foo = 1;
submethod BUILD {
::is($foo, 2, 'Test2 - $foo from parameter after default BUILD for $foo');
}
}
class Test3 {
has $.foo = 1;
submethod BUILD (:$bar) {
::is($foo, 1, 'Test3 - $foo from attribute after default BUILD for $foo');
}
}
class Test4 {
has $.foo = 1;
submethod BUILD (:$bar) {
::is($foo, 2, 'Test4 - $foo from parameter after default BUILD for $foo');
}
}
class Test5 {
has $.foo = 1;
submethod BUILD (:$foo) { # what should happen here? attribute or parameter?!
::is($foo, 2, 'Test5 - $foo from parameter while BUILD for $foo');
}
}
class Test6 {
has $.foo = 1;
submethod BUILD (:foo($newfoo)) {
::is($newfoo, 2, 'Test6 - $newfoo from parameter while BUILD for $foo');
::is($foo, 1, 'Test6 - $foo from attribute while BUILD for $foo'); # $foo should not be set per default BUILD for $foo
$foo = $newfoo;
}
}
::is 'Test1'->new( )->get_foo(), 1 => 'Test2 - $foo still from attribute';
::is 'Test2'->new(foo => 2 )->get_foo(), 2 => 'Test2 - $foo from parameter';
::is 'Test3'->new( bar => 2)->get_foo(), 1 => 'Test3 - $foo still from attribute';
::is 'Test4'->new(foo => 2, bar => 2)->get_foo(), 2 => 'Test4 - $foo from parameter';
::is 'Test5'->new(foo => 2 )->get_foo(), 2 => 'Test5 - $foo from parameter';
::is 'Test6'->new(foo => 2 )->get_foo(), 2 => 'Test6 - $foo from parameter';
done_testing();