Skip to content

Instantly share code, notes, and snippets.

@masak
Created August 6, 2010 21:48
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 masak/512060 to your computer and use it in GitHub Desktop.
Save masak/512060 to your computer and use it in GitHub Desktop.
From f288966040c6b106259db621c5c64992e8f57bc7 Mon Sep 17 00:00:00 2001
From: Carl Masak <cmasak@gmail.com>
Date: Fri, 23 Jul 2010 01:46:17 +0200
Subject: [PATCH] [src/glue/enum.pm] return the right things
enum Foo <a b c>;
Previously, requesting Foo yielded an EnumMap, whereas it now yields an
object of some kind whose .enums method returns an EnumMap. Similarly,
requesting b yielded an Int, whereas now it returns an object that responds
to a number of methods, as per S12.
---
src/glue/enum.pm | 28 ++++++++++++++++++++++++----
1 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/src/glue/enum.pm b/src/glue/enum.pm
index 419da11..0594b00 100644
--- a/src/glue/enum.pm
+++ b/src/glue/enum.pm
@@ -4,11 +4,31 @@ our sub SETUP_NAMED_ENUM($name, $values) {
# For now, just install EnumMap under the main name.
my @full_ns = Perl6::Grammar::parse_name($name);
my ($shortname, @base_ns) = @full_ns;
- pir::set_hll_global__vPSP(@base_ns, $shortname, $values);
-
+ my $enumeration-object = (class {
+ method WHAT { $enumeration-object }
+ method enums { $values }
+ method Str { $name }
+ multi method pick(:$replace) { $values.pick($replace) }
+ multi method pick($num, :$replace) { $values.pick($num, $replace) }
+ method ACCEPTS($topic) { $topic eqv any $values.values }
+ method defined { False }
+ }).new;
+ pir::set_hll_global__vPSP(@base_ns, $shortname, $enumeration-object);
+
for $values.kv -> $key, $value {
- pir::set_hll_global__vPSP(@full_ns, $key, $value);
- pir::set_hll_global__vPSP(@base_ns, $key, $value);
+ my $enum-object = $value but role {
+ method WHAT { $enumeration-object }
+ method perl { $name ~ '::' ~ $key }
+ method Str { $name ~ '::' ~ $key }
+ method Stringy { $key }
+ method key { $key }
+ method value { $value }
+ method pair { $key => $value }
+ method kv { $key, $value }
+ method defined { True }
+ };
+ pir::set_hll_global__vPSP(@full_ns, $key, $enum-object);
+ pir::set_hll_global__vPSP(@base_ns, $key, $enum-object);
}
}
--
1.7.0.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment