Perl-Apollo / Corinna

Corinna - Bring Modern OO to the Core of Perl
Artistic License 2.0
157 stars 19 forks source link

Multi Role Application Feedback #17

Closed Ovid closed 3 years ago

Ovid commented 4 years ago

This ticket is for feedback on Proposal: Multiple Role Application.

tobyink commented 4 years ago

Here's how it can be done in Zydeco. Zydeco doesn't support renaming methods composed from roles, but it does support parameterized roles. (Like MooseX::Role::Parameterized or Package::Variant+Moo::Role.)

Generally I dislike renaming methods from roles because it means that $object->does($rolename) no longer guarantees that all the methods from the role's API will be available.

use v5.16;

package Local::App {
    use Zydeco;

    role CounterRole ( Str $get, Str $inc ) {
        has $counter (
            type        => PositiveOrZeroInt,
            default     => 0,
            reader      => $get,
            handles_via => 'Counter',
            handles     => [ $inc => 'inc...' ],
        );
    }

    class TwoCounters does CounterRole('value1', 'inc_first'),
                           CounterRole('value2', 'inc_second')
    {
        method to_string {
            sprintf "counter1 = %d; counter2 = %d", $self->value1, $self->value2;
        }

        overload q("") => 'to_string', fallback => true;
    }
}

my $c = Local::App->new_twocounters;
$c->inc_first->inc_first->inc_second;
say $c;
Ovid commented 3 years ago

This is resolved for the MVP. Further issues should be new tickets.