Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Give enums the capability to produce a role based on themselves.
  • Loading branch information
jnthn committed May 8, 2012
1 parent 9cdfbfa commit d2e01f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Perl6/Metamodel/EnumHOW.pm
Expand Up @@ -27,6 +27,10 @@ class Perl6::Metamodel::EnumHOW

# Roles that we do.
has @!does_list;

# Role'd version of the enum.
has $!role;
has int $!roled;

my $archetypes := Perl6::Metamodel::Archetypes.new( :nominal(1), :composalizable(1) );
method archetypes() {
Expand Down Expand Up @@ -99,8 +103,14 @@ class Perl6::Metamodel::EnumHOW
$obj
}

my $composalizer;
method set_composalizer($c) { $composalizer := $c }
method composalize($obj) {
nqp::die("Cannot yet turn an enum into a role");
unless $!roled {
$!role := $composalizer($obj, self.name($obj), %!values);
$!roled := 1;
}
$!role
}

method does_list($obj) {
Expand Down
16 changes: 16 additions & 0 deletions src/core/Enumeration.pm
Expand Up @@ -63,3 +63,19 @@ sub ANON_ENUM(*@args) {
nqp::getattr(%res, EnumMap, '$!storage'));
$r;
}

Metamodel::EnumHOW.set_composalizer(-> $type, $name, %enum_values {
my Mu $r := Metamodel::ParametricRoleHOW.new_type(:name($name));
$r.HOW.add_attribute($r, Attribute.new(
:name('$!' ~ $name), :type(nqp::p6decont($type)),
:has_accessor(1), :package($r)));
for %enum_values.kv -> $key, $value {
my $meth = method () { self."$name"() === $value }
$meth.set_name($key);
$r.HOW.add_method($r, $key, $meth);
}
$r.HOW.set_body_block($r,
-> |$c { nqp::list($r, nqp::hash('$?CLASS', $c<$?CLASS>)) });
$r.HOW.compose($r);
$r
});

0 comments on commit d2e01f1

Please sign in to comment.