develooper Front page | perl.perl6.users | Postings from January 2019

Re: Roles are fundamentally broken?

Thread Previous | Thread Next
January 30, 2019 06:47
Re: Roles are fundamentally broken?
Message ID:
> Yet, have a look at my example with private methods

All of the bug reports, code excerts use regular (public) methods. Do you
have code to share with !private_methods and/or submethods? I just made an
example, will be at the end of this email

> If accidentally two roles declare same private method – I either must
reject using one of them or resolve the conflict manually ...

The docs say what to do if two roles declare the same regular method. I
would expect the same to hold for private methods, and submethods- the
consuming class would need to define its own same-named submethod or
private method, calling the whichever role routine(s) it needs.

it sounds like you'd like to have a role be able to declare some routines
which only other methods in the role can use- not the consuming class, or
any other role. Turns out that private methods in roles do the trick!

use v6;

# Turn anything into a public entertainer
role Diva {
  method captivate (&talent) {
    say "tais toi!";

  method !bow {
    say "<takes a bow>"

# Learn to play
role Violinist {
  method fiddle {
    say "got sheet music, got rosin...";

  method !bow {
    say "<world's most beautiful melodies>"

# cute and playful
class Puppy {
  method greet {
   say "<sniff>";

  method !bow {
    say "Bow wow wow!"

my $best_friend=( but Violinist) but Diva;

*Bow wow wow!*
*tais toi!*
*got sheet music, got rosin...*
*<world's most beautiful melodies>*
*<takes a bow>*

I'm very happy with that behavior. Would like it to be documented.

(And, I'll add comments to your bug tickets now)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About