alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (bug267.scala)

This example Scala source code file (bug267.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

base, base, baseplus, exp, exp, exp1, exp1, int, num, num, num1, plus, plus1, show

The Scala bug267.scala source code

package expAbstractData

/** A base class consisting of
 *   - a root trait (i.e. abstract class) `Exp' with an `eval' function
 *   - an abstract type `exp' bounded by `Exp'
 *   - a concrete instance class `Num' of `Exp' for numeric literals
 */
trait Base {
  type exp <: Exp

  trait Exp {
    def eval: Int
  }
  class Num(v: Int) extends Exp { self: exp =>
    val value = v
    def eval = value
  }
}

object testBase extends App with Base {
  type exp = Exp
  val term = new Num(2);
  Console.println(term.eval)
}

/** Data extension: An extension of `Base' with `Plus' expressions
 */
trait BasePlus extends Base {
  class Plus(l: exp, r: exp) extends Exp { self: exp =>
    val left = l
    val right = r
    def eval = left.eval + right.eval
  }
}

/** Operation extension: An extension of `Base' with 'show' methods.
 */
trait Show extends Base {
  type exp <: Exp1

  trait Exp1 extends Exp {
    def show: String
  }
  class Num1(v: Int) extends Num(v) with Exp1 { self: exp with Num1 =>
    def show = value.toString()
  }
}

/** Operation extension: An extension of `BasePlus' with 'show' methods.
 */
trait ShowPlus extends BasePlus with Show {
  class Plus1(l: exp, r: exp) extends Plus(l, r) with Exp1 { self: exp with Plus1 =>
    def show = left.show + " + " + right.show
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala bug267.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.