Skip to content

Instantly share code, notes, and snippets.

@tobnee
Created March 7, 2011 09:12
Show Gist options
  • Save tobnee/858262 to your computer and use it in GitHub Desktop.
Save tobnee/858262 to your computer and use it in GitHub Desktop.
Easy ways to pimp you Java with Scala while being fully compatible with the Java ecosystem
import scala.reflect.BeanProperty
// Easy ways to pimp you Java with Scala while being fully compatible with the Java ecosystem
// java bean scala
case class Person(@BeanProperty var name:String,
@BeanProperty var alter:Int) {
import System.{currentTimeMillis => currTime}
protected val creationTime = currTime
def lifeTime = currTime - creationTime
def wasBinIchInXjahren(j:Int=1) = alter + j
}
// scala way to define a debug version of the class without having to change
// the main impl
trait PersonDebug { self:Person =>
override def toString = super.toString + " creationTime: "+self.creationTime
}
def run() {
val p1 = new Person("Heinz", 20)
println(p1) // nice println generated by case class
//@BeanProperty generates setters (if using var/val)
//and getters (if useing var)
p1.setAlter(21)
println(p1)
val p2 = new Person("Tobias", 25)
println(p2)
// equals / hashCode is also generated
println("p1 and p2 are"+(if (p1 != p2) " not ")+ "equal")
val p1debug = new Person("Heinz", 20) with PersonDebug
println(p1debug)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment