Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnumHOW inner workings: creating enums without Enumeration? #2165

Open
JJ opened this issue Aug 1, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@JJ
Copy link
Contributor

commented Aug 1, 2018

The Problem

enums can be created using the enumHOW protocol:

my %pairings = %( A => "T",
                  T => "A",
                  C => "G",
                  G => "C" );


constant length = 3;
enum Chain ( A => "T", G => "C", T => "A");
for ^length {
    my $base = Chain.pick;
    say "{$base.key} with {$base.value} and {$base.succ.pair}";
}


enum Chain2 ();
for <A C G T>.roll( length ) -> $letter {
    Chain2.HOW.add_enum_value( Chain2, $letter => %pairings{$letter} );
}

for ^length {
    my $base = Chain2.pick;
    say "{$base.key} with {$base.value}";
}

However, the behavior of the first, "naturally" created enum is different from the second. The first returns Enumeration objects, while the second returns just Pairs. Enumeration objects do have the prev and succ methods, while other objects need not have that.

Expected Behavior

add_enum_value uses anything with a key and value. When an enum is built as a type, Pairs are converted to Enumerations (I assume). When you use add_enum_value, they are admitted without conversion.
The expected behavior would make them behave consistently, with Pairs converted to Enumeration or with that role mixed in.

Actual Behavior

Building enums step by step is going to behave differently from the declaration.

Steps to Reproduce

Run above program with any Perl 6

JJ added a commit to perl6/doc that referenced this issue Aug 1, 2018

Adds new page for enumeration.
Along the way,

* [this question in
SO](https://stackoverflow.com/questions/51632394/assigning-a-value-to-the-attribute-of-a-mixed-in-role),
* Rakudo issue rakudo/rakudo#2165 on behavior of enumHOW
* Rakudo issue rakudo/rakudo#2156 on behavior of enumHOW again.
* A failed PR trying to change the behavior of Enumeration.

It is not complete, because some stuff would have to change in Rakudo
first, but it's there. So this closes #1555
@FCO

This comment has been minimized.

Copy link
Member

commented Aug 2, 2018

This code seems to work as expected:

my %pairings = %( A => "T",
                  T => "A",
                  C => "G",
                  G => "C" );


constant length = 3;
enum Chain ( A => "T", G => "C", T => "A");
for ^length {
    my $base = Chain.pick;
    say "{$base.key} with {$base.value} and {$base.succ.pair}";
}


enum Chain2 ();
for <A C G T>.roll( length ) -> $letter {
    Chain2.HOW.add_enum_value( Chain2, Chain2.bless: key => $letter, value => %pairings{$letter} );
}
Chain2.^compose_values;
for ^length {
    my $base = Chain2.pick;
    say "{$base.key} with {$base.value} and {$base.succ.pair}";
}

Looks that the enum values must be a instance of that enum type.

@JJ

This comment has been minimized.

Copy link
Contributor Author

commented Aug 5, 2018

enum values have to be an instance of Enumeration, not enum, which is a set of Enumerations. But the problem is that they don't become that automatically, only when you use ^compose_values. That means that they are somehow turned into Enumerations when composed, but I don't see why that couldn't happen when they are being added in add_enum_value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.