-
-
Save Altai-man/b97ba9d8a054fae9309e16bf55ae392b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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