title: How to pass hash-like object which does associative role to a constructor expecting a hash?
body: <p>I am experimenting with <a href="https://docs.perl6.org/language/subscripts#Methods_to_implement_for_associative_subscripting" rel="noreferrer">customized hashes</a>. The following is trying to implement a simpler lookup for config-like hashes:</p>
  
  <pre><code>use v6;
  
  class X::Config::KeyNotFound is Exception {
      method message() {
          "Key not found!";
      }
  }
  
  # A hash that allows for nested lookup using a '.' to separate keys.
  # (This means that keys themselves cannot contain a dot)
  # For example:
  #
  #   %h = Config.new(%(a =&gt; %(b =&gt; 1)));
  #   my $foo = %h&lt;a.b&gt;;  # &lt;-- $foo = 1
  #
  class Config does Associative[Cool,Str] {
      has %.hash;
  
      multi method AT-KEY ( ::?CLASS:D: $key) {
          my @keys = $key.split('.');
          my $value = %!hash;
          for @keys -&gt; $key {
              if $value{$key}:exists {
                  $value = $value{$key};
              }
              else {
                  X::Config::KeyNotFound.new.throw;
              }
          }
          $value;
      }
  
      multi method EXISTS-KEY (::?CLASS:D: $key) {
          my @keys = $key.split('.');
          my $value = %!hash;
          for @keys -&gt; $key {
              if $value{$key}:exists {
                  $value = $value{$key};
              }
              else {
                  return False;
              }
          }
          return True;
      }
  
      multi method DELETE-KEY (::?CLASS:D: $key) {
          X::Assignment::RO.new.throw;
      }
  
      multi method ASSIGN-KEY (::?CLASS:D: $key, $new) {
          X::Assignment::RO.new.throw;
      }
  
      multi method BIND-KEY (::?CLASS:D: $key, $new){
          X::Assignment::RO.new.throw;
      }
  }
  
  my %hash = a =&gt; %(aa =&gt; 2, ab =&gt; 3), b =&gt; 4;
  my %cfg := Config.new( hash =&gt; %hash );
  
  # A dummy class to illustrate the problem:    
  class MyTest {
      has %.config;
  }
  
  # Now this code does not work:
  MyTest.new(
      config  =&gt; %cfg,
  );
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Odd number of elements found where hash initializer expected:
  Only saw: Config.new(hash =&gt; {:a(${:aa(2), :ab(3)}), :b(4)})
    in block &lt;unit&gt; at ./p.p6 line 70
  </code></pre>
  
  <p>(Line 70 is the line <code>MyTest.new(</code> )</p>
  
  <p>The code works fine if I pass a normal hash to the constructor instead, for example using <code>%hash</code> instead of <code>%cfg</code>:</p>
  
  <pre><code>MyTest.new(
      config  =&gt; %hash,
  );
  </code></pre>