Skip to content

Instantly share code, notes, and snippets.

@Altai-man
Created January 9, 2019 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Altai-man/b97ba9d8a054fae9309e16bf55ae392b to your computer and use it in GitHub Desktop.
Save Altai-man/b97ba9d8a054fae9309e16bf55ae392b to your computer and use it in GitHub Desktop.
use nqp;
sub create_enum_value($enum_type_obj, $key, $value, $index) {
# Create directly.
my $val := nqp::rebless(nqp::clone($value), $enum_type_obj);
nqp::bindattr($val, $enum_type_obj, '$!key', $key);
nqp::bindattr($val, $enum_type_obj, '$!value', $value);
nqp::bindattr_i($val, $enum_type_obj, '$!index', $index);
# Add to meta-object.
$enum_type_obj.^add_enum_value($val);
# Result is the value.
$val
}
# Create an enum
my $new-enum = Metamodel::EnumHOW.new_type(name => 'Foos', base_type => Int);
$new-enum = $new-enum but Enumeration;
my $value = create_enum_value($new-enum, 'Foo', 2, 1);
# Test part
say $new-enum.^name; # Foos+{Enumeration}
say $value.^name; # Foos+{Enumeration}
say $value ~~ $new-enum; # Has to be True => True
say $new-enum ~~ $value; # Has to be False => False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment