Raku / book

Using Raku – an unfinished book about Raku
https://perl6book.com/
284 stars 49 forks source link

Chapter 6 example doesn't work with rakudo star 2011.01 #49

Closed jeteve closed 13 years ago

jeteve commented 13 years ago

Hi,

I've just tried:

multi to-json(Real $d) {
$d;
}
multi to-json(Bool $b) {
  $b ?? 'true' !! 'false';
}
multi to-json(Str $s){
'"'
    ~ $s.trans( ['"' , '\\' ] => [ '\"' , '\\\\' ])
    ~ '"';
}

multi to-json(Array $d) {
  return '[ '
  ~ $d.values.map({  to-json($_) }).join(', ')
    ~ ' ]';
 }
 multi to-json(Hash $d) {
   return '{ '
    ~ $d.pairs.map({ to-json(.key)
                     ~ ' : '
                 ~ to-json(.value) }).join(', ')
                   ~ ' }';
 }

When I do

say to-json({ a => @a , b => 1.23 });

perl6 chokes:

too few positional arguments: 2 passed, 3 (or more) expected
in 'to-json' at line 13:chapter6.p6
in <anon> at line 24:chapter6.p6
in 'Any::join' at line 1
in 'to-json' at line 26:chapter6.p6
in main program body at line 47:chapter6.p6

For some reason it looks like it doesn't like what's given to the 'map' method.

moritz commented 13 years ago

Thanks for your report, this is actually a bug in Rakudo. I've fixed it in https://github.com/rakudo/rakudo/commit/405afa981857f7aeef7bcebe83a79ae4d934d1da so it should work with the next rakudo release.