Skip to content

Instantly share code, notes, and snippets.

@stephenLee
Created September 18, 2012 11:28
Show Gist options
  • Save stephenLee/3742681 to your computer and use it in GitHub Desktop.
Save stephenLee/3742681 to your computer and use it in GitHub Desktop.
Scala Traits mixins example
import scala.collection.mutable.ArrayBuffer
//Abstract class IntQueue
abstract class IntQueue {
def get(): Int
def put(x: Int)
}
// A BasicIntQueue implemented with an ArrayBuffer
class BasicIntQueue extends IntQueue {
private val buf = new ArrayBuffer[Int]
def get() = buf.remove(0)
def put(x: Int) { buf += x }
}
// The Doubling stackable modification trait.
// super calls in a trait are dynamically bound.
trait Doubling extends IntQueue {
abstract override def put(x: Int) { super.put(2 * x) }
}
trait Incrementing extends IntQueue {
abstract override def put(x: Int) { super.put(x + 1) }
}
trait Filtering extends IntQueue {
abstract override def put(x: Int) {
if (x >= 0) super.put(x)
}
}
// Mixing in a trait when instantiating with new
val queue = new BasicIntQueue with Doubling
queue.put(10)
queue.get()
// The order of mixins is significant, traits further to the
// right take effect first.
val queue1 = (new BasicIntQueue with Incrementing with Filtering)
queue1.put(-1)
queue1.put(0)
queue1.put(1)
queue1.get() // 1
queue1.get() // 2
@zhangruichang
Copy link

zhangruichang commented Dec 23, 2016

This is same with sample here.
http://www.artima.com/scalazine/articles/stackable_trait_pattern.html
mixins and trait is same as stackable trait pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment