6 use Test::More tests => 5;
15 This test demonstrates how simple it is to create Scala Style
16 Class Mixin Composition. Below is an example taken from the
17 Scala web site's example section, and trancoded to Moose.
19 L<http://scala.epfl.ch/intro/mixin.html>
21 A class can only be used as a mixin in the definition of another
22 class, if this other class extends a subclass of the superclass
23 of the mixin. Since ColoredPoint3D extends Point3D and Point3D
24 extends Point2D which is the superclass of ColoredPoint2D, the
25 code above is well-formed.
27 class Point2D(xc: Int, yc: Int) {
30 override def toString() = "x = " + x + ", y = " + y;
33 class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
35 def setColor(newCol: String): Unit = color = newCol;
36 override def toString() = super.toString() + ", col = " + color;
39 class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
41 override def toString() = super.toString() + ", z = " + z;
44 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
45 extends Point3D(xc, yc, zc)
46 with ColoredPoint2D(xc, yc, col);
49 Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())
51 "x = 1, y = 2, z = 3, col = blue"
59 has 'x' => (is => 'rw');
60 has 'y' => (is => 'rw');
64 "x = " . $self->x . ", y = " . $self->y;
67 package ColoredPoint2D;
72 has 'color' => (is => 'rw');
76 $self->SUPER . ', col = ' . $self->color;
84 has 'z' => (is => 'rw');
88 $self->SUPER . ', z = ' . $self->z;
91 package ColoredPoint3D;
95 with 'ColoredPoint2D';
99 my $colored_point_3d = ColoredPoint3D->new(x => 1, y => 2, z => 3, color => 'blue');
100 isa_ok($colored_point_3d, 'ColoredPoint3D');
101 isa_ok($colored_point_3d, 'Point3D');
102 isa_ok($colored_point_3d, 'Point2D');
104 is($colored_point_3d->to_string(),
105 'x = 1, y = 2, z = 3, col = blue',
106 '... got the right toString method');