Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does polymorphism work with undefined intermediate types in C#?

In the following code, I would have expected calling a.Generate(v) would have resulted in calling V.Visit(A a), since when Generate is called this is of type A. Hoewever, it appears that this is seen as being an Inter instead.

Is it possible to have the intended behaviour without explicitly implementing the (identical) method in both A and B and only on the shared base class? If so, how can it be acheived?

using System;
using System.Diagnostics;

namespace Test {
    class Base {}
    class Inter: Base {
        public virtual void Generate(V v) {
            // `Visit(Base b)` and not `Visit(A a)` is called when calling
            // A.Generate(v). Why?
            v.Visit(this);
        }
    }

    class A: Inter {}
    class B: Inter {}

    class V {
        public void Visit(Base b) { throw new NotSupportedException(); }
        public void Visit(A a)    { Trace.WriteLine("a"); }
        public void Visit(B b)    { Trace.WriteLine("b"); }
    }

    class Program {
        static void Main() {
            V v = new V();
            A a = new A();
            B b = new B();

            a.Generate(v);
            b.Generate(v);
        }
    }
}

Edit It was suggested in the answers that the code above is not polymorphic. I would object to that. V.Visit is polymorphic.

like image 828
joce Avatar asked Jan 30 '12 19:01

joce


1 Answers

You are expecting the call to v.Visit(this) to determine which overload of Visit to call based on the run time types of both v and this.

Languages which have that feature are called "double virtual dispatch" languages. (Or, if more than two things are considered they are called "multiple virtual dispatch" languages, or "multimethod" languages.)

C# is not a double-virtual dispatch language; it is a single-virtual dispatch language when dispatch decisions are made at compile time. That is, the decision of which overload to choose is made on the basis of the runtime type of the receiver, but the compile-time types of the arguments.

Now, in your case, C# does not use single virtual dispatch because the call to Visit is not even a virtual call in the first place! The fact that the call to Generate was a virtual call is completely irrelevant, and the fact that Visit is overloaded is also irrelevant. The dispatch to Visit is made non-virtually, so the dispatch logic is based entirely on the compile-time type of the receiver, v, and the argument this. Since the receiver is known to be of type V and the argument is known to be of type Inter, overload resolution must choose the best possible match given only that information. It cannot choose the versions of Visit that take A or B because those are more derived than the known argument type, Inter. It must choose the overload with the less derived formal parameter type, Base.

If you wish to achieve double-virtual dispatch in C#, there are two standard ways to do it. First, you can use dynamic; with dynamic, the analysis is performed at runtime using the runtime types. Simply cast the receiver and the arguments to dynamic and the compiler will take care of it for you. That imposes a significant performance cost though.

The second standard way to do it is to use the Visitor Pattern, which you can find out about by searching the internet for it. I suspect based on the names of your methods that you are trying to implement the Visitor Pattern already; this is not the right way to do it.

like image 147
Eric Lippert Avatar answered Nov 02 '22 23:11

Eric Lippert