Can Perl 6 use the % sigil for something other than a Hash?

Viewed 316

The Perl 6 docs on variables notes that the % sigil can be used with types that do the Associative role. It specifically mentions Pair, Hash, and Map. But, how would I get a Pair into a variable with the %?

If I use a type constraint I get a curious error:

> my Pair %pair = Pair.new( 'a', 'b' )
Type check failed in assignment to %pair; expected Pair but got Str ("b")
  in block <unit> at <unknown file> line 1

If I assign without the type constraint I get a hash:

my %pair = Pair.new: 'a', 'b';  # Hash, not Pair

Binding works:

my %pair := Pair.new: 'a', 'b';  # Pair

But if I use a type constraint, I get another curious error:

> my Pair %p2 := Pair.new: 'a', 'b';
Type check failed in binding; expected Associative[Pair] but got Pair (:a("b"))
  in block <unit> at <unknown file> line 1

The same problem shows up with Bag and Set. Do it with Map and you end up with a mutable Hash.

I figure there's a variety of issues here, but perhaps the % sigil isn't as versatile as I was led to believe.

3 Answers
Related