```class A class B open class C open class D val a...
# announcements
c
Copy code
class A
class B
open class C
open class D
val a: A = A()
val c: C = C()

a is B // <-- as expected: error
c is D // <-- not expected: no error
As expected I get an "Incompatible types B and A"-error at compile time for the line
a is B
. But not for the next line
c is D
. Is there any chance that
c
could be of type
D
or is this a missing feature in the compiler? Does anyone have an example?
d
Someone could later introduce
D
as a superclass of
C
.
That is not possible in the first case, because
B
is not open (i.e. final).
c
Thanks.... but how could I introduce a superclass later?
I have the feeling I oversee something, but I can't create a common subclass of C and D and/or hide from the compiler that these classes might already have some common super classes - or can I?
d
class C : D()
Now
c is D
is true.
And yes, the compiler could know that this is not a case and make it an error, however that would be somewhat strange, as normally introducing a superclass is not seen as a backwards-incompatible change, but with this it would be.
m
the
is
keyword probably only tests on open-ness of a class. Even though we can logically conclude
c is D
to not be the case in this specific instance. nothing can
is
a final class, and thus you get an error