title: Instantiate a Raku class, and update an instance variable in the constructor
body: <p>I seem to be having difficulty understanding how to correctly work with classes in Raku.</p>
  <p>I am trying to create a 'Database' class that I will use throughout my cro application. However I don't seem to understand how to deal with setting instance variables at object construction (BUILD?) time.</p>
  <p>The class below shows an example of the problem I am having when calling it with:</p>
  <p>Calling:</p>
  <pre><code>my $db = Database.new(dsn =&gt; 'update me!');
  say &quot;what value is foo: &quot; ~ $db.what-is-foo(); # prints &quot;what value is foo: 0&quot; but expecting 123
  </code></pre>
  <p>My Class:</p>
  <pre><code>use DB::Pg;
  
  class Database {
      has Str    $.dsn is required;
      has DB::Pg $!dbh = Nil;
      has Int    $!foo = 0;
  
      method new (:$dsn) {
          my $dbh = DB::Pg.new(conninfo =&gt; :$dsn);
          my $foo = 123;
          return self.bless(:$dsn, dbh =&gt; $dbh, foo =&gt; $foo);
      }
  
      method what-is-foo() {
          return $!foo;
      }
  }
  </code></pre>
  <p>So in my class constructor I would like to pass in a named arg for the database dsn.
  In the new method I plan to connect, and set an instance variable to the connection handle.</p>
  <p>In this example I am using a simple integer (foo) as a test.</p>
  <p>So far I have found <a href="https://docs.raku.org/language/classtut" rel="noreferrer">the docs here</a> to not include an example of this type of pattern, except pehaps:</p>
  <pre><code>use DB::Pg;
  
  class Database {
      has Str    $.dsn is required;
      has DB::Pg $!dbh = Nil;
      has Int    $!foo = 0;
  
      submethod BUILD(:$dsn) {
          $!dbh = DB::Pg.new(conninfo =&gt; :$dsn);
          $!foo = 123;
      }
  
      method what-is-foo() {
          return $!foo;
      }
  }
  </code></pre>
  <p>But that gives me:</p>
  <pre><code>The attribute '$!dsn' is required, but you did not provide a value for it.
  </code></pre>
  <p>Any help would be greatly appreciated!</p>