Skip to content

Instantly share code, notes, and snippets.

@zerobase
Last active December 11, 2015 12:38
Show Gist options
  • Save zerobase/4602113 to your computer and use it in GitHub Desktop.
Save zerobase/4602113 to your computer and use it in GitHub Desktop.
What I learned on Scala: * Traits with a same private var name does not conflict. * Traits with a same method name do conflict.
/**
* Traits with a same private var name does not conflict.
* Traits with a same method name do conflict.
*/
trait Countable {
private var _count:Int = 0
def count:Int = _count
def countup:Unit = _count += 1
}
trait Sizable {
private var _count:Int = 0
def size:Int = _count
def sizeup:Unit = _count += 1
// def count:Int = size // uncomment this and you get the error below:
/**
* error: overriding method count in trait Countable of type => Int;
* method count in trait Sizable of type => Int needs `override' modifier
* val container = new Container with Countable with Sizable
* ^
*/
}
class Container {
}
val container = new Container with Countable with Sizable
println("before: countainer.countup")
println("countainer.count = " + container.count)
println("countainer.size = " + container.size)
container.countup
println("after: countainer.countup")
println("countainer.count = " + container.count)
println("countainer.size = " + container.size)
/**
* result:
* before: countainer.countup
* countainer.count = 0
* countainer.size = 0
* after: countainer.countup
* countainer.count = 1
* countainer.size = 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment