Here's a code snippet that tries to use a subset to constrain the
values of an object field (i.e. declared with has).  As written, this
code works, but only when there's what looks like an irrelevant
experimental line in it (labeled "WEIRD ONE"), when that line is
commented out it throws an error...

  class HasSubset {
      my @allowed = << alpha beta gamma delta >>;
      my @default = << alpha >>;

      subset Allowed of Str where * eq any( @allowed );

      my Allowed $experiment = 'delta'; # WEIRD ONE this line is
*needed* to get the following to work...

      has Allowed @.greek = @default;

      method echo_greek {
          say @!greek.join(" | ");
      }
  }

  my $obj = HasSubset.new();
  $obj.echo_greek();

  # As written, prints the default: 'alpha'
  # Without the WEIRD ONE line, you see errors like:
  ## Type check failed in assignment to @!greek; expected
HasSubset::Allowed but got Str ("alpha")

Reply via email to