Ovid wrote:
That works just fine, so I know we have types on signatures. But now
this fails:
# remove the 'Int' and this works
sub fact (Int $n) {
if 0 == $n {
return 1;
}
else {
return $n * fact($n - 1);
}
}
say fact(5);
The failure is at runtime, not compile time (full output below), but
I'm not sure how to proceed since I don't know if this is supposed to
work yet. Do I consult tests? Follow the Rakudo blog? Religiously
read this list? (the latter's not much of an option because I love to
take breaks from email from time to time).
Cheers,
Ovid
Null PMC access in type()
current instr.: 'fact' pc 334 (EVAL_15:127)
called from Sub '_block11' pc 34 (EVAL_15:17)
called from Sub 'parrot;PCT::HLLCompiler;eval' pc 806
(src/PCT/HLLCompiler.pir:481)
called from Sub 'parrot;PCT::HLLCompiler;evalfiles' pc 1088
(src/PCT/HLLCompiler.pir:610)
called from Sub 'parrot;PCT::HLLCompiler;command_line' pc 1267
(src/PCT/HLLCompiler.pir:699)
called from Sub 'parrot;Perl6::Compiler;main' pc 12318 (perl6.pir:174)
perl6(27829) malloc: *** Deallocation of a pointer not malloced:
0x31c1750; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
perl6(27829) malloc: *** Deallocation of a pointer not malloced:
0x6bb04b; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
I have encountered this bug also. It is caused by the fact that result
of $n-1 is Num not Int. So if you cast it to Int it will work:
return $n * fact(int $n - 1);
I do not know where is it source of the bug - the addition op that
produces Num, not Int or the type check.
Luben