7 'SUPER' => 1.10, # skip all if not installed
12 This test demonstrates how simple it is to create Scala Style
13 Class Mixin Composition. Below is an example taken from the
14 Scala web site's example section, and trancoded to Class::MOP.
17 We require SUPER for this test to handle the issue with SUPER::
18 being determined at compile time.
20 L<http://scala.epfl.ch/intro/mixin.html>
22 A class can only be used as a mixin in the definition of another
23 class, if this other class extends a subclass of the superclass
24 of the mixin. Since ColoredPoint3D extends Point3D and Point3D
25 extends Point2D which is the superclass of ColoredPoint2D, the
26 code above is well-formed.
28 class Point2D(xc: Int, yc: Int) {
31 override def toString() = "x = " + x + ", y = " + y;
34 class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
36 def setColor(newCol: String): Unit = color = newCol;
37 override def toString() = super.toString() + ", col = " + color;
40 class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
42 override def toString() = super.toString() + ", z = " + z;
45 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
46 extends Point3D(xc, yc, zc)
47 with ColoredPoint2D(xc, yc, col);
50 Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())
52 "x = 1, y = 2, z = 3, col = blue"
56 use Scalar::Util 'blessed';
60 # fetch the metaclass for the
61 # caller and the mixin arg
62 my $metaclass = (caller)->meta;
63 my $mixin = (shift)->meta;
65 # according to Scala, the
66 # the superclass of our class
67 # must be a subclass of the
68 # superclass of the mixin (see above)
69 my ($super_meta) = $metaclass->superclasses();
70 my ($super_mixin) = $mixin->superclasses();
71 ($super_meta->isa($super_mixin))
72 || confess "The superclass must extend a subclass of the superclass of the mixin";
74 # collect all the attributes
75 # and clone them so they can
76 # associate with the new class
77 my @attributes = map {
78 $mixin->get_attribute($_)->clone()
79 } $mixin->get_attribute_list;
82 my $method = $mixin->get_method($_);
83 # we want to ignore accessors since
84 # they will be created with the attrs
85 (blessed($method) && $method->isa('Class::MOP::Method::Accessor'))
86 ? () : ($_ => $method)
87 } $mixin->get_method_list;
90 # I assume that locally defined methods
91 # and attributes get precedence over those
94 # add all the attributes in ....
95 foreach my $attr (@attributes) {
96 $metaclass->add_attribute($attr)
97 unless $metaclass->has_attribute($attr->name);
100 # add all the methods in ....
101 foreach my $method_name (keys %methods) {
102 $metaclass->add_method($method_name => $methods{$method_name})
103 unless $metaclass->has_method($method_name);
111 Point2D->meta->add_attribute('$x' => (
116 Point2D->meta->add_attribute('$y' => (
123 $class->meta->new_object(@_);
128 "x = " . $self->x . ", y = " . $self->y;
131 package ColoredPoint2D;
132 our @ISA = ('Point2D');
134 ColoredPoint2D->meta->add_attribute('$color' => (
141 $self->SUPER() . ', col = ' . $self->color;
145 our @ISA = ('Point2D');
147 Point3D->meta->add_attribute('$z' => (
154 $self->SUPER() . ', z = ' . $self->z;
157 package ColoredPoint3D;
158 our @ISA = ('Point3D');
160 ::with('ColoredPoint2D');
164 my $colored_point_3d = ColoredPoint3D->new(x => 1, y => 2, z => 3, color => 'blue');
165 isa_ok($colored_point_3d, 'ColoredPoint3D');
166 isa_ok($colored_point_3d, 'Point3D');
167 isa_ok($colored_point_3d, 'Point2D');
169 is($colored_point_3d->toString(),
170 'x = 1, y = 2, z = 3, col = blue',
171 '... got the right toString method');