John Lam 1997 — 2026

More method overloading madness in RubyCLR

Yikes. This method overloading + shadow class inheritance problem turns out to be even harder than it looks. I suspect anyone writing code to interop with .NET are going to run straight into this problem.

Here’s a description of this new scenario:

``` class A { public int M1() { return 1; } } class B : A { public new int M1() { return 1; } } ```

If you create an invoke M1 via an A object reference:

A.new.M1

You will have created the initial shim method on the A shadow class.

Now if you try to invoke M1 on B via:

B.new.M1

This will call via the M1 shim method on class A. Right now I rely on the method_missing calls to generate shim methods on demand. Clearly this can’t be the case for dispatching these kinds of non-virtual methods that can be redefined in derived classes.

It seems to me that the method shim would have to see whether the object reference being called is derived from the “most derived” class that the shim knows about. If not, it will have to redefine the method shim on the fly at runtime and bind it to the shadow class that the invocation is happening through.

Yuck. This is a LOT of code (and a fundamental redesign of the code since there are lots of assumptions baked in about this kind of behavior) to handle this corner case that I suspect will be pretty rare out there in the wild.

Comments welcome about the “most derived” algorithm would be welcome.

3 archived reader comments

Sam Smoot (2006/07/19)

It might just be me, but my personal feeling is that it’d be great to have more of the common use cases taken care of. I don’t really use RubyClr to mess with interfaces for example. In fact, given the option, I’d do my best to avoid them completely since it’s very un duck-like. Of course I realize you can’t tackle issues we don’t report so I’ll try to be a little more dilligent in that respect. And that’s for the new release! :) Here’s a quick repo: As a general rule, RubyClr doesn’t seem to handle collections correctly. I can do a datatable.columns.each for example, but I can’t do a datatable.rows.each. I can do this: class Nullifier { public object value = null; } n = Nullifier.new n.value == nil But I can’t do this without seg-faulting: class Nullifier { public ArrayList value = new ArrayList(); } n = Nullifier.new n.value.add 1 n.value[0] == 1 # true dat n.value.add nil n.value[1] == nil # crashification! And then there’s the non-blittable thing… So keep up the great work! I really appreciate it!

John Lam (2006/07/19)

Sam, I just tried to repro your test here: class CollectionTests < TestCase inline :csharp do |compiler| compiler.compile <<-EOF using System.Collections; namespace RubyClr.Tests { public class Nullifier { public ArrayList value = new ArrayList(); } } EOF end def test_collection_bug n = Nullifier.new n.value.add 1 assert_equal 1, n.value[0] n.value.add nil assert_equal nil, n.value[1] end end It seems to work fine …

Sam Smoot (2006/07/24)

My bad. I coulda sworn this was an issue, but maybe the identity fix resolved it? Sorry about that. I’ll try to get my facts straight next time (and use the latest code of course!). DataTable#Rows#each still has issues for me however, but I’ll try to get a real bug-repo together for ya. Thanks again for the hard work!