8 plan skip_all => "SUPER 1.10 required for this test" if $@;
13 This test demonstrates how simple it is to create Scala Style
14 Class Mixin Composition. Below is an example taken from the
15 Scala web site's example section, and trancoded to Class::MOP.
18 We require SUPER for this test to handle the issue with SUPER::
19 being determined at compile time.
21 L<http://scala.epfl.ch/intro/mixin.html>
23 A class can only be used as a mixin in the definition of another
24 class, if this other class extends a subclass of the superclass
25 of the mixin. Since ColoredPoint3D extends Point3D and Point3D
26 extends Point2D which is the superclass of ColoredPoint2D, the
27 code above is well-formed.
29 class Point2D(xc: Int, yc: Int) {
32 override def toString() = "x = " + x + ", y = " + y;
35 class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
37 def setColor(newCol: String): Unit = color = newCol;
38 override def toString() = super.toString() + ", col = " + color;
41 class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
43 override def toString() = super.toString() + ", z = " + z;
46 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
47 extends Point3D(xc, yc, zc)
48 with ColoredPoint2D(xc, yc, col);
51 Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())
53 "x = 1, y = 2, z = 3, col = blue"
57 use Scalar::Util 'blessed';
61 # fetch the metaclass for the
62 # caller and the mixin arg
63 my $metaclass = (caller)->meta;
64 my $mixin = (shift)->meta;
66 # according to Scala, the
67 # the superclass of our class
68 # must be a subclass of the
69 # superclass of the mixin (see above)
70 my ($super_meta) = $metaclass->superclasses();
71 my ($super_mixin) = $mixin->superclasses();
72 ($super_meta->isa($super_mixin))
73 || confess "The superclass must extend a subclass of the superclass of the mixin";
75 # collect all the attributes
76 # and clone them so they can
77 # associate with the new class
78 my @attributes = map {
79 $mixin->get_attribute($_)->clone()
80 } $mixin->get_attribute_list;
83 my $method = $mixin->get_method($_);
84 # we want to ignore accessors since
85 # they will be created with the attrs
86 (blessed($method) && $method->isa('Class::MOP::Method::Accessor'))
87 ? () : ($_ => $method)
88 } $mixin->get_method_list;
91 # I assume that locally defined methods
92 # and attributes get precedence over those
95 # add all the attributes in ....
96 foreach my $attr (@attributes) {
97 $metaclass->add_attribute($attr)
98 unless $metaclass->has_attribute($attr->name);
101 # add all the methods in ....
102 foreach my $method_name (keys %methods) {
103 $metaclass->add_method($method_name => $methods{$method_name})
104 unless $metaclass->has_method($method_name);
112 Point2D->meta->add_attribute('$x' => (
117 Point2D->meta->add_attribute('$y' => (
124 $class->meta->new_object(@_);
129 "x = " . $self->x . ", y = " . $self->y;
132 package ColoredPoint2D;
133 our @ISA = ('Point2D');
135 ColoredPoint2D->meta->add_attribute('$color' => (
142 $self->SUPER() . ', col = ' . $self->color;
146 our @ISA = ('Point2D');
148 Point3D->meta->add_attribute('$z' => (
155 $self->SUPER() . ', z = ' . $self->z;
158 package ColoredPoint3D;
159 our @ISA = ('Point3D');
161 ::with('ColoredPoint2D');
165 my $colored_point_3d = ColoredPoint3D->new(x => 1, y => 2, z => 3, color => 'blue');
166 isa_ok($colored_point_3d, 'ColoredPoint3D');
167 isa_ok($colored_point_3d, 'Point3D');
168 isa_ok($colored_point_3d, 'Point2D');
170 is($colored_point_3d->toString(),
171 'x = 1, y = 2, z = 3, col = blue',
172 '... got the right toString method');