You need to use this.type instead of classOf[this] .
class Shape { var mName: String = null def named(name: String): this.type = { mName = name this } } class Rectangle extends Shape { }
Now, to demonstrate that it works (in Scala 2.8)
scala> new Rectangle().named("foo") res0: Rectangle = Rectangle@33f979cb scala> res0.mName res1: String = foo
this.type is the type name of the compiled type, and classOf is the statement that is called at run time to get the java.lang.Class object. You cannot use classOf[this] ever, because the parameter must be a type name. Your two parameters, when trying to get the java.lang.Class object, should call classOf[TypeName] or this.getClass() .
Ken bloom
source share