@@ -669,9 +669,9 @@ say RectangleWithCachedArea.new( x2 => 5, x1 => 1, y2 => 1, y1 => 0).area;
669
669
670
670
= head2 Object cloning
671
671
672
- The cloning is done using the L < clone|/routine/clone > method available on all objects, which
673
- shallow-clones both public and private attributes. New values for I < public >
674
- attributes can be supplied as named arguments.
672
+ The cloning is done using the L < clone|/routine/clone > method available on all
673
+ objects, which shallow-clones both public and private attributes. New values for
674
+ I < public > attributes can be supplied as named arguments.
675
675
676
676
= begin code
677
677
class Foo {
@@ -685,43 +685,47 @@ attributes can be supplied as named arguments.
685
685
say $o2; # Foo.new(foo => 42, bar => 5000)
686
686
= end code
687
687
688
- See document for L < clone|/routine/clone > for details on how non-scalar attributes get cloned,
689
- as well as examples of implementing your own custom clone methods.
688
+ See document for L < clone|/routine/clone > for details on how non-scalar
689
+ attributes get cloned, as well as examples of implementing your own custom clone
690
+ methods.
690
691
691
- = head1 The X < role > declarator
692
- X < |does >
692
+ = head1 X < Roles|declarator,role >
693
693
694
694
Roles are a collection of attributes and methods; however, unlike classes, roles
695
695
are meant for describing only parts of an object's behavior; this why, in
696
696
general, roles are intended to be I < mixed in > classes and objects. In general,
697
697
classes are meant for managing objects and roles are meant for managing behavior
698
698
and code reuse within objects.
699
699
700
- = begin code
701
- use MONKEY-SEE-NO-EVAL;
702
- role Serializable {
703
- method serialize() {
704
- self.perl; # very primitive serialization
705
- }
706
- method deserialize($buf) {
707
- EVAL $buf; # reverse operation to .perl
708
- }
709
- }
700
+ X < |does > Roles use the keyword C < role > preceding the name of the role that is
701
+ declared. Roles are mixed in using the C < does > keyword preceding the name of the
702
+ Role that is mixed in.
710
703
711
- class Point does Serializable {
712
- has $.x;
713
- has $.y;
704
+ = begin code
705
+ use MONKEY-SEE-NO-EVAL;
706
+ role Serializable {
707
+ method serialize() {
708
+ self.perl; # very primitive serialization
714
709
}
715
- my $p = Point.new(:x(1), :y(2));
716
- my $serialized = $p.serialize;
717
- my $clone-of-p = Point.deserialize($serialized);
718
- say $clone-of-p.x; # OUTPUT: «1»
719
- = end code
710
+ method deserialize($buf) {
711
+ EVAL $buf; # reverse operation to .perl
712
+ }
713
+ }
714
+
715
+ class Point does Serializable {
716
+ has $.x;
717
+ has $.y;
718
+ }
719
+ my $p = Point.new(:x(1), :y(2));
720
+ my $serialized = $p.serialize;
721
+ my $clone-of-p = Point.deserialize($serialized);
722
+ say $clone-of-p.x; # OUTPUT: «1»
723
+ = end code
720
724
721
725
Roles are immutable as soon as the compiler parses the closing curly brace of
722
726
the role declaration.
723
727
724
- = head2 Application of role
728
+ = head2 Applying roles
725
729
726
730
Role application differs significantly from class inheritance. When a role
727
731
is applied to a class, the methods of that role are copied into the class.
0 commit comments