Shadow class inheritance can break in RubyCLR due to method overloading. The problem has to do with redefining methods in derived classes and relating that back to the problem of Ruby not allowing method overloading.
Consider this set of C# classes:
```
class A {
public int M1() { return 1; }
public int M1(int x) { return 2; }
}
class B : A { }
class C : B {
public new int M1() { return 3; }
}
```
Here are some interesting ways of calling M1():
```
A a = new A();
B b = new B();
C c = new C();
Console.WriteLine("new A().M1() = {0}, new A().M1(1) = {1}", a.M1(), a.M1(1));
Console.WriteLine("new B().M1() = {0}, new B().M1(1) = {1}", b.M1(), b.M1(1));
Console.WriteLine("new C().M1() = {0}, new C().M1(1) = {1}", c.M1(), c.M1(1));
Console.WriteLine("(new C() as A).M1() = {0}", (c as A).M1());
```
This poses some design issues for how methods are dispatched in RubyCLR:
- The method shim (M1) is defined on A’s shadow class and calls M1() and M1(int). This method shim will be inherited by B’s shadow class.
- A new method shim (M1’) is defined on class C’s shadow class and calls C::M1() and A::M1(int).
- There must be some way to call A::M1() from a reference to an instance of C.
The code from above in Ruby would look like:
```
assert_equal 1, A.new.M1
assert_equal 2, A.new.M1(1)
assert_equal 1, B.new.M1
assert_equal 2, B.new.M1(1)
assert_equal 3, C.new.M1
assert_equal 2, C.new.M1(1)
```
Now the last case is the hard one syntactically -
assert_equal 1, C.new.M1_of_A
Would something like _of_(declaring class) as the name of the shim that calls the correct method the right way of doing this?
Comments appreciated!
1 archived reader comment
John Lam (2006/07/19)
Hmmm … I worry a bit about this approach because it messes with object identity (which I just fixed for everything except interfaces!) The only way I could make it work is to @dup@ the object and set its class to the @A@ shadow class: c = C.new c_as_a = c.as A Now @c@ and @c_as_a@ now point to the same CLR object even though they are in reality two different Ruby objects. I think this would get confusing in scenarios that involve collections of these things. BTW, this also means that I’m thinking about ways to get rid of the @as@ operator for interfaces. I still have to solve the problem for interface-only methods that can’t be accessed through the class interface, but that’s another story …