On 07/03/2015 07:20 PM, Tom Browder wrote: > On Fri, Jul 3, 2015 at 10:26 AM, Tom Browder <tom.brow...@gmail.com> wrote: >> While experimenting I've found the first two methods of passing a hash >> to a subroutine work: >> >> # method 1 >> my %hash1; >> foo1(%hash1); >> say %hash1.perl; >> sub foo1(%hash) { >> %hash{1} = 0; >> } > Another question on method 1 above, please: > > What is the proper type to use for the %hash for a more complete > signature in function foo1? I've tried various types such > Associative, Hash, and Any and gotten an exception. > > Thanks, > > -Tom When you define a parameter with a % sigil or a @ sigil (or even a & sigil) in your signature, you'll get the right kind of type restriction "for free".
Try this: > perl6 -e 'sub foobar(%i) { }; foobar((1, 2, 3))' > Type check failed in binding %i; expected 'Associative' but got 'Parcel' If you declare an additional Type to be matched against, you parameterize the Associative (or Positional in case of an @ sigil) type you require to be passed. Something that very often surprises newcomers is that this works: > sub takes_int_array(Int @bar) { say @bar } > my Int @foo = 1, 2, 3; > takes_int_array(@foo) but this does not > sub takes_int_array(Int @bar) { say @bar } > takes_int_array([1, 2, 3]) because the type match is against the defined type. We do not automatically infer that [1, 2, 3] could be a Positional[Int]. Hope that helps and I've also successfully predicted your next stumbling block for you :) - Timo