The REPL seems a little strange, but if you really compile or interpret the script, this really points to the current instance of the surrounding object.
import scala.reflect.runtime.{ universe => ru } object Main { def getType[T : ru.TypeTag](instance: T) = ru.typeOf[T] def sayHello = println("hello!") def main(args: Array[String]): Unit = { println(this.getType(123)) // Prints "Int" this.sayHello // Prints "hello!" to the console getType(this).decls foreach println _ // Prints the following outputs to the console: // constructor Main // method getType // method sayHello // method main } }
As to why this behavior does not show up in REPL, I'm not sure.
source share