Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |