The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

This test demonstrates how simple it is to create Scala Style Class Mixin Composition. Below is an example taken from the Scala web site's example section, and trancoded to Class::MOP.

NOTE: We require SUPER for this test to handle the issue with SUPER:: being determined at compile time.

http://scala.epfl.ch/intro/mixin.html

A class can only be used as a mixin in the definition of another class, if this other class extends a subclass of the superclass of the mixin. Since ColoredPoint3D extends Point3D and Point3D extends Point2D which is the superclass of ColoredPoint2D, the code above is well-formed.

  class Point2D(xc: Int, yc: Int) {
    val x = xc;
    val y = yc;
    override def toString() = "x = " + x + ", y = " + y;
  }

  class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
    val color = c;
    def setColor(newCol: String): Unit = color = newCol;
    override def toString() = super.toString() + ", col = " + color;
  }

  class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
    val z = zc;
    override def toString() = super.toString() + ", z = " + z;
  }

  class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
        extends Point3D(xc, yc, zc)
        with ColoredPoint2D(xc, yc, col);


  Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())

  "x = 1, y = 2, z = 3, col = blue"