How do I define the class of an object in Scala?

I need to check that y is strictly an instance of bar AND NOT foo . How do I do this in Scala?

trait foo {}

trait bar extends foo {}

val x = new foo {}
val y = new bar {}

x.isInstanceOf[foo] // true
x.isInstanceOf[bar] // false

y.isInstanceOf[bar] // true
y.isInstanceOf[foo] // true (but I want it to return false)

      

+3


source to share


2 answers


There is a class in the title of your question, but the actual question uses traits. You can do something like this through runtime reflection with classes. Let's create a convenience method to get the reflect.runtime.universe.Type

object:

import scala.reflect.runtime.universe._

def tpeOf[A](a: A)(implicit tt: TypeTag[A]): Type = tt.tpe

      

And some examples of classes:

class Foo
class Bar extends Foo
val x = new Foo
val y = new Bar

      



We can use our method tpeOf

to get Type

from x

and y

and compare it to Type

TypeTag

that obtained with typeOf

. This will give the desired results.

scala> tpeOf(x) =:= typeOf[Foo]
res0: Boolean = true

scala> tpeOf(x) =:= typeOf[Bar]
res1: Boolean = false

scala> tpeOf(y) =:= typeOf[Foo]
res2: Boolean = false

scala> tpeOf(y) =:= typeOf[Bar]
res3: Boolean = true

      

But that doesn't work for traits because in your example y

it is not an instance bar

, it is an instance of an anonymous class that extends bar

. Therefore using this method will always give false

.

trait foo {}
trait bar extends foo {}
val x = new foo {}
val y = new bar {}

scala> tpeOf(x) =:= typeOf[bar]
res4: Boolean = false   // As expected, `x` is not exactly `bar`

      

+3


source


getClass

will work if you just create new instances. But you are creating new anonymous classes and instances of those.



For those involved with foo and bar, it's the same: they are superclasses.

+3


source







All Articles