I want to say "if a function takes a B
, then directly calling a method that B
does not have is an error".
This seems like an obvious statement in terms of programming to an interface. But what about downcasting? Consider the following Python code.
class C:
...
class B:
def b_func() -> C:
...
class A(B):
def a_func() -> C:
...
def f(b: B) -> C
if isinstance(b, A):
return b.a_func()
else:
return b.b_func()
At runtime, the call to b.a_func()
is perfectly fine because we checked the runtime type, so in that sense it is not an error. However, this feels very wrong in terms of programming to the B
interface. In what sense is calling a method that B
does not have an error, even if the instance of B
happens to have a runtime type that does have that method? What would be the name of that kind of error? Or is my intuition completely wrong?