Rewriting introduction mixin example for Scala2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Rewriting introduction mixin example for Scala2

griba2010
Hi!

Because I ran into problems using the Scala1 intro mixin examples
(http://scala.epfl.ch/intro/mixin.html) with Eclipse plugin 2.x

I've tried to rewrite the code for Scala2 showing some new features

------------------------

package test;

/*
Scala2 replacement code
for http://scala.epfl.ch/intro/mixin.html
*/


class Point2D(xc: Int, yc: Int) {
  val x = xc
  val y = yc

  // Scala2 now automatically applies methods
  // with empty parameter lists
  // to () argument lists when necessary
  // so we can write 'toString' instead of 'toString()'

  override def toString =
    "x = " + x + ", y = " + y
}

class Point3D(xc: Int, yc: Int, zc: Int)
    extends Point2D(xc, yc) {
  // Linearization (inheritance class sequence)
  // = { Point3D ,Point2D, AnyRef, Any}
  val z = zc
  override def toString =
    super.toString + ", z = " + z
}

// mixins cannot have class parameters
// so we have to extract the code from Scala1 ColoredPoint2D
// in order to use it as mixin

mixin class Colorable {
  var color: String = _

  def setColor(newCol: String): Unit =
    color = newCol

  override def toString =
    // super in a mixin class refers to the base class
    // in a mixin inheritance sequence (also called class Linearization)
    super.toString + ", col = " + color

    def mostVisible = "Color"
}

class ColoredPoint2D( xc: Int, yc: Int, c: String)
    extends Point2D( xc, yc) with Colorable {
  // Linearization (inheritance class sequence)
  // = { ColoredPoint2D ,Colorable ,Point2D, AnyRef, Any}
  setColor( c)
}

class ColoredPoint3D( xc: Int, yc: Int, zc: Int, c: String)
    extends Point3D( xc, yc, zc) with Colorable {
  // Linearization (inheritance class sequence)
  // = { ColoredPoint3D ,Colorable, Point3D ,Point2D, AnyRef, Any}
  setColor( c)
}

// a second mixin with an abstract function

abstract mixin class Markable {
  var mark: String = _
  def setMark( newMark: String): Unit =
    mark = newMark

  override def toString =
    super.toString + ", mark = " + mark

  // overloading: an abstract member will not override
  // a concrete matching member in a mixin inheritance seq.
  // e.g.:"with Colorable with Markable"
  def mostVisible: String
}

class MarkedColoredPoint2D( xc: Int, yc: Int
    , c: String, m: String)
    extends Point2D( xc, yc)
    with Colorable with Markable {
  // Linearization (inheritance class sequence)
  // = { MarkedColoredPoint2D, Markable, Colorable, Point2D, AnyRef, Any}

setColor( c) ;
setMark( m)

// the overloaded abstract member mostvisible in Markable
// will not override the concrete one in Colorable
}

class MarkedColoredPoint3D( xc: Int, yc: Int, zc: Int
                          , c: String, mark_p: String, mostVisible_p:String)
                          extends Point3D( xc, yc, zc)
                          with Colorable with Markable {
  // Linearization (inheritance class sequence)
  // = { MarkedColoredPoint3D, Markable, Colorable, Point3D, Point2D, AnyRef, Any}

  setColor( c) ;
  setMark( mark_p)

  override def mostVisible = mostVisible_p
}

object MixTest {
  def main(args: Array[String]): Unit = {

    Console.println(
        new ColoredPoint2D(1, 2, "red").toString) ;

    Console.println(
        new ColoredPoint3D(3, 4, 5, "green").toString)

    val mcp2D = new MarkedColoredPoint2D(6, 7, "blue", "gas station")

    Console.println( mcp2D.toString)

    Console.println( "mcp2D most visible: "+ mcp2D.mostVisible)

    val mcp3D = new MarkedColoredPoint3D(8, 9, 10, "yellow", "sight-seeing", "Panorama")

    Console.println( mcp3D.toString)

    Console.println( "mcp3D most visible: "+ mcp3D.mostVisible)
  }
}

----- end of example

I have learned a bit of Scala2 doing this.

I hope it helps.

Cheers! Salut!

Gabriel