Greg Donald : Roles - an alternate form of class inheritance Page

Roles - an alternate form of class inheritance

Roles are collections of methods and attributes that can be mixed into classes. A role provides an alternate form of code reuse from inheritance. Roles are typically mixed in using "does".

role Seller {
  method sell { say self.^name ~ ' is selling' }
}

role Buyer {
  method buy { say self.^name ~ ' is buying' }
}

class Consumer does Buyer {}
class Producer does Seller {}
class Company does Buyer does Seller {}

my $producer = Producer.new;
my $consumer = Consumer.new;
my $company = Company.new;

$producer.sell;
$consumer.buy;
$company.sell;
$company.buy;


Output:

Producer is selling
Consumer is buying
Company is selling
Company is buying

raku (16)