This example is simplified but demonstrates how the error message is misleading.
Type check failed in assignment to $!val; expected
in block
my constant @GOOD-VALUES = <these are acceptable values>;
sub check-param($val --> True ) {
for @GOOD-VALUES -> $check-val {
return if $val.Str eq $check-val;
}
die 'Not an accepted value';
}
class A {
has $.val is required where check-param($_);
}
my $a = A.new(val => 'acceptable');
Line 4 should be "return True" instead of just "return" but the error message didn't lead me to look there at all.
Pointing the line number at Line 4 or even line 2 would be an improvement. Stating "expected Bool::True" or "expected Bool" instead of "expected
Frankly, I'm not actually sure what return is returning in that line; is it $val or is it $check-val?
return is like return false.
($val --> Bool ) behave the same. The dynamic check-param is returning false. So what do you think of ?
__Before:__
Type check failed in assignment to $!val; expected <anon> but got Str ("acceptable")
__After:__
Dynamic check failed in assignment to $!val; check-param($val --> Bool) returned False
I think that would have identified exactly where to make a change; so a huge improvement.
A smaller test
sub check-param($val --> Bool ) { return False; }
class A { has $.val where check-param($_); }
my $a = A.new(val => 'unacceptable');
Most helpful comment
I think that would have identified exactly where to make a change; so a huge improvement.