6 use Test::More tests => 1;
14 This test demonstrates how simple it is to create Scala Style
15 Class Mixin Composition. Below is an example taken from the
16 Scala web site's example section, and trancoded to Class::MOP.
19 We require SUPER for this test to handle the issue with SUPER::
20 being determined at compile time.
22 L<http://scala.epfl.ch/intro/mixin.html>
24 A class can only be used as a mixin in the definition of another
25 class, if this other class extends a subclass of the superclass
26 of the mixin. Since ColoredPoint3D extends Point3D and Point3D
27 extends Point2D which is the superclass of ColoredPoint2D, the
28 code above is well-formed.
30 class Point2D(xc: Int, yc: Int) {
33 override def toString() = "x = " + x + ", y = " + y;
36 class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
38 def setColor(newCol: String): Unit = color = newCol;
39 override def toString() = super.toString() + ", col = " + color;
42 class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
44 override def toString() = super.toString() + ", z = " + z;
47 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
48 extends Point3D(xc, yc, zc)
49 with ColoredPoint2D(xc, yc, col);
52 Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())
54 "x = 1, y = 2, z = 3, col = blue"
62 Point2D->meta->add_attribute('$x' => (
67 Point2D->meta->add_attribute('$y' => (
74 $class->meta->new_object(@_);
79 "x = " . $self->x . ", y = " . $self->y;
82 package ColoredPoint2D;
83 our @ISA = ('Point2D');
85 ColoredPoint2D->meta->add_attribute('$color' => (
92 $self->SUPER() . ', col = ' . $self->color;
96 our @ISA = ('Point2D');
98 Point3D->meta->add_attribute('$z' => (
105 $self->SUPER() . ', z = ' . $self->z;
108 package ColoredPoint3D;
109 our @ISA = ('Point3D');
111 ::with('ColoredPoint2D');
115 my $colored_point_3d = ColoredPoint3D->new(x => 1, y => 2, z => 3, color => 'blue');
116 isa_ok($colored_point_3d, 'ColoredPoint3D');
117 isa_ok($colored_point_3d, 'Point3D');
118 isa_ok($colored_point_3d, 'Point2D');
120 is($colored_point_3d->toString(),
121 'x = 1, y = 2, z = 3, col = blue',
122 '... got the right toString method');