How does `isInstanceOf` work?
Asked Answered
H

2

32

Assume, we have:

class B
class A extends B
trait T

Then it holds:

val a: A with T = new A with T 
a.isInstanceOf[B]  // result is true !

Is it right to say, the isInstanceOf method checks, if there is at least one type (not all types) which matches the right hand side in a subtype relationship?

At first look, I thought a value with type A with T can not be a subtype of B, because A and T are not both subtypes of B. But it is A or T is a subtype of B -- is that right ?

Hilliard answered 2/7, 2012 at 8:29 Comment(1)
Here a quick overview of object equality joelabrahamsson.com/…Douglasdouglashome
P
42

isInstanceOf looks if there is a corresponding entry in the inheritance chain. The chain of A with T includes A, B and T, so a.isInstanceOf[B] must be true.

edit:

Actually the generated byte code calls javas instanceof, so it would be a instanceof B in java. A little more complex call like a.isInstanceOf[A with T] would be (a instanceof A) && (a instanceof T).

Pellikka answered 2/7, 2012 at 8:35 Comment(2)
Thank you for the background information, that helped a lot. Do you know where I can find more background information about the Scala's isInstanceOf method ? The Scala API does not tell much about it.Hilliard
There's not much to tell. It is just instanceof.Pellikka
H
13

At first look, I thought a value with type A with T can not be a subtype of B

There's are two misconceptions here. First, that the static type of an instance has any bearing on the result of isInstanceOf: there is none. To be clear, when doing a.isInstanceOf[B], the fact that a is of type A with T is not relevant.

The method isInstanceOf is implemented at the bytecode level by the JVM. It looks at the class information every instance carries, and checks whether B one of the classes (the class of the instance itself and its ancestors), or one of the implemented interfaces. That's the "is-a" relationship: "a is a B".

Technically, isInstanceOf is part of Java's reflection, where it is known as instanceof.

The second misconception is the inheritance can somehow remove a parent type. That never happens: inheritance only adds types, never removes them. The type A with T is an A, a B, a T, an AnyVal and an Any. So even if isInstanceOf did look at the type A with T, it would still return true.

Herzen answered 2/7, 2012 at 13:58 Comment(2)
Do you know how i can prove Null and Nothing are sub-types of all things in the REPL? "A".isInstanceOf[Null] errors? as does Nothing? I guess that makes sense; ie "A" instanceof null wouldn't work in Java. Do I just have to take the docs word for it?!Marybethmaryellen
@Marybethmaryellen Nothing does not exist in Java, since it treats classes and primitives and distinct things, and instanceof only pertains to the former. Also, Null does not exist either in Java, which has no concept of a "bottom".Herzen

© 2022 - 2024 — McMap. All rights reserved.