Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Throw in some initial thoughts on what enumerations implemented in Pe…
…rl 6 could look like. Rakudo actually can compile this, but we don't include it in core yet, and it certainly won't quite work yet either.
  • Loading branch information
jnthn committed Dec 7, 2009
1 parent e07f8d5 commit 2069f24
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/core/enum.pm
@@ -0,0 +1,58 @@
role Enum[$key, $value, $enumeration] {
method key() {
$key
}

method value() {
$value
}

method WHAT() {
$enumeration
}
}

# XXX Just defining this here, but Rakudo will need it properly before we
# can actually use any of this code.
class EnumMap { }

class Enumeration {
has $!name;
has $.enums;

method new(::UnderlyingType, $name, @values) {
# Create instance.
my $self = self.bless(*, :name($name));

# Create all of the enum values.
my %value_objects;
my UnderlyingType $current;
for @values -> $value {
my ($key, $val);
if $value ~~ Pair {
$key = $value.key;
$current = $val = $value.value;
}
else {
$key = $value;
$val = $current++;
}
%value_objects{$key} = $val but Enum[$key, $val, $self];
}

# Set enum map in place.
pir::setattribute__vPsP($self, '$!enums', EnumMap.new(%value_objects));
}

method defined() {
0
}

method WHAT() {
self
}

method Str() {
$!name;
}
}

0 comments on commit 2069f24

Please sign in to comment.