Raku Signature Errors with Arrays & Hashes

A quick post to help future Raku geeks understand a couple of confusing error messages:

expected Positional[Array] but got Array

and

expected Associative[Hash] but got Hash

These are conceptually the same problem. If you've received one of these errors it means you've double-specified your parameter by using the @ or % and Array or Hash.

A parameter of Hash %foo says "I would like to be passed something that implements Associative and has a Hash in it." (a Hash of Hashes). Likewise Array @foo says "I would like to be passed something that implements Positional and has an Array in it". Yes, that sounds backwards. No, I can't explain why.

To solve this for hashes you either need to specifiy Hash OR you need to use %, but not both. To solve this for arrays you either need to specify Array OR you need to use @, but not both.

# This won't work
sub bad-hash-signature(Hash %foo){
  say("this will error when passed a simple Hash");
}
# Solution 1
sub good-hash-signature(%foo){
  say("hash was " ~ %foo.raku);
}
# Solution 2
sub alternate-good-hash-signature(Hash $foo){
  say("hash was " ~ $foo.raku);
}

Now, to be extra pedantic, "Solution 1" will allow anything that implements the Associative role. If you really want to lock it down to only accept Hash you need to go with "Solution 2". The same idea applies to the Positional role and arrays.