Archive for February, 2010

Scala traits

Really love scala,
traits and the ability of define custom types as interfaces.


type ObjectWithName = {
    def getName: String
}

trait NicePerson[T <: ObjectWithName] {
    def greet: String = {
        "Hello ! My name is %s".format(this.asInstanceOf[ObjectWithName].getName)
    }
}

class Person(name: String) {
    def getName: String = name
}

val peter = new Person("Peter") with NicePerson[Person]
println(peter.greet) // = Hello ! My name is Peter


How nice it is ?

Some references:
http://markthomas.info/blog/?p=92
http://codemonkeyism.com/scala-goodness-structural-typing/