score:0

how about making t a method and making that generic.

  import scala.reflect.runtime.universe._
  class base {
    def myactualtype[t <: base](b: t)(implicit tt: typetag[t]) = typeof[t]
  }
  class foo extends base 
  class bar extends foo 
  val bar = new bar
  assert(bar.myactualtype(bar) =:= typeof[bar])

the downside is that you always have to send the object reference to it when you call it, but you get what you want.

score:2

it's unfortunately a common misunderstanding of this.type: it isn't the class of the instance, it's the singleton type (i.e. the type which only has one member: this). it won't work without inheritance either.

this can be done using f-bounded polymorphism:

class base[a <: base[a] : typetag] {
  val t = typeof[a]
}

class x extends base[x]

Related Query

More Query from same tag