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]
Source: stackoverflow.com
Related Query
- How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?
- I want to get the type of a variable at runtime
- How the get the classOf for a scala object type
- Get companion object of class by given generic type Scala
- How to get rid of : class type required but T found
- How do I get an instance of the type class associated with a context bound?
- Why do I get "expected class or object definition" when defining a type in scala?
- Get the specific simple name of a generic type in Scala
- In Scala Reflection, How to get generic type parameter of a concrete subclass?
- How to get the actual type of a generic type?
- Is it possible to define a macro with variadic parameters, and get a type for each parameter?
- Get type of a "singleton type"
- How to get evidence that each type element in Shapeless HList belongs to a typeclass
- How to get Kotlin's type safe builders to work in Scala?
- Get elements of type structure of row by name in SPARK SCALA
- How do I get the runtime Class of a parameterized Type in a Scala trait
- How to get the actual type of a generic function in Scala?
- Scala override method with subclass as parameter type
- Why do I get "pattern type is incompatible with expected type"?
- How to get the classtag of a path-dependent type
- Specific parameter type in Subclass not possible
- scala pattern match a function - how to get around type erasure
- How the sample Simple IO Type get rid of side effects in "FP in Scala"?
- Get function type from scala console
- Shapeless: is there a way to get Hlist type from a case class or a tuple
- Using type lambdas together with higher-kinded types in Scala: how to get compiler to infer types correctly?
- Scala 2.10 reflection: Why do I get the same type "List()" for the list and list element?
- Scala macros: Get type arguments of root type of object members inside class definition
- Get Java reflection representation of Scala type
- Cannot get type of generic object in a list
More Query from same tag
- Spark SQL Dataframes - using $columnname in where clause of a join works in spark-shell but doesn't compile
- Scala tree recursive fold method
- How to run Scala code in SBT Build.scala just before compile of Scala files?
- How to assign a name to intermediate pattern of a List?
- Scala - how to build an immutable map from a collection of Tuple2s?
- In Scala reflection, why reflection function on TypeTag still has type erasure?
- Loading a JNI library in scala
- Why are List and String identifiers named "xs" (in Scala and other languages)?
- Convert Akka Iterable to java.lang.Iterable?
- Will standalone scala program takes advantage of distributed/parallel processing? or does spark Scala require separate code?
- What's the final version of Scala3's enum syntax?
- Getting org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException): No lease on in par
- Case class: performances of always copying vs check for differences before copying
- Running shell script as a part of spark job
- Why are Scala's `Lists` implemented as linked lists
- Reuse Scala type as bounds in method definition
- Combining elements on an rdd of tuple5 based on one of the entires in tuple in scala
- Scala underScore strange behavior: error: missing parameter type for expanded function
- SBT not producing jar file
- pass dynamically created data to d3 through playframework
- Verifying a PKCS#7 Signature with BouncyCastle in Scala
- logic and if inside match case in scala
- Setting up actions for multiple test folders in SBT using 'Simple' configuration
- Scala parameterized function won't compile
- Running multiple shell commands with wildcards
- Scala slick perform complex sql query
- Convert List[(Int,String)] into List[Int] in scala
- Scala Specs2 Matchers with "aka" doesn't work
- using impilicits to make apply method easier to use
- How to calculate total across columns but one?