0

Type variance is often mentioned in the context of generic types, but it is also applicable when talking about subtyping of classes. From what I have learnt about variance and subtyping, it seems like the following must be true:

If class B is a subclass of class A, then methods defined for class B must take arguments which are supertypes of the arguments taken by the corresponding methods in class A (if the methods are defined for A).

At the same time, a common notion in OOP is that a method is merely a function which takes an object of the class as one of its arguments, albeit usually in a syntactically obscured way.

This means, that the methods of the subclass do not only take supertypes as arguments, at least not in this shallow understanding.

My theory is that you could say that a method used in subtyping and polymorphism is slightly more than a simple function. It feels like, in C++ terms, the virtual method on its own is a function which takes an object of the superclass and, using virtual dispatch, returns a function object with the class object captured, which now has the appropriate type (a function type which takes supertypes and returns subtypes, because it no longer takes the object as one of its arguments).

I do not know if this image is a fair representation of reality, though. I am looking for two things:

  1. The truth, i.e. confirmation that my imagination is correct or the actual theory behind this problem, and
  2. A citable source which states the truth.

I've seen this post which touches on the problem in the question, but then the answers and comments focus heavily on behavioural subtyping, when I suppose I am more interested in type-theoretic subtyping.

Maurycyt
  • 676
  • 3
  • 19
  • I don't see how answering this question one way or another changes anything in practice. "If my imagination is correct then X is possible, otherwise X is impossible". Can you show an example of X? – n. m. could be an AI May 29 '23 at 10:23
  • I do not need to solve any problem in practice. I need a theoretical model and citable source, as stated in the question. – Maurycyt May 29 '23 at 12:00
  • You can use either model because they both describe the same reality, so just use whatever is convenient. – n. m. could be an AI May 29 '23 at 13:16
  • Citable is convenient. I am looking for a source I can cite. – Maurycyt May 29 '23 at 15:06
  • After re-reading your question, I find an error in your reasoning. "...common notion in OOP is that a method is merely a function which takes an object of the class as one of its arguments" It is not, The object of the class is a special kind of argument, a *dispatching* one. The actual function to be called depends at the run-time type of that argument. The actual type of that special argument must be a *sub* type of the declared type. In some languages a function can have more than one dispatching argument (multiple dispatch). – n. m. could be an AI May 29 '23 at 15:36
  • (cont) Dispatching arguments go sub, non-dispatching arguments go super. You can find citable sources in the Wikipedia article on multiple dispatch, the names of interest are Castagna and Cardelli. I'm not sure any of this has anything to do with whether you represent a method as a bound function or not. – n. m. could be an AI May 29 '23 at 15:40

0 Answers0