My earlier implementation of interfaces for RubyCLR was incomplete. I didn’t have code to let you test whether an object implemented the interface requested. There are a couple of approaches that you can use to solve this problem: via reflection at runtime and via a shim method that “knows” about all of the interfaces that an object implements.
I chose the second option: implementing two new methods is and as. The goal is to eventually provide the same semantics as the C# is and as operators. You can use the is method to test whether an object implements an interface, and you can use the as operator to return a reference to that interface.
I use Reflection to enumerate all of the interfaces of a class and bake in all of those interface references by recording the RuntimeTypeHandle for each one of the interfaces. This approach gives me superior performance since I avoid using Reflection on all but the first call to the is or as methods of a type.
The ultimate goal is that is and as will match the semantics of is and as in C#. Currently I’m missing support for the boxing and unboxing scenarios as well as the super-type scenario.
Here’s the unit test for the is method:
def test_is
m = System::Data::DataSet.new
assert m.is?(System::Data::DataSet)
assert m.is?(IComponent)
assert m.is?(IDisposable)
assert m.is?(IServiceProvider)
assert m.is?(IListSource)
assert m.is?(System::Xml::Serialization::IXmlSerializable)
assert m.is?(ISupportInitializeNotification)
assert m.is?(ISupportInitialize)
assert m.is?(System::Runtime::Serialization::ISerializable)
assert !m.is?(System::Data::IColumnMapping)
end
and a simpler test for the as method:
def test_as
a = ArrayList.new
a.add(1)
e = a.as(IEnumerable).get_enumerator.as(IEnumerator)
assert e.move_next
assert 1, e.current
assert !e.move_next
assert_equal nil, a.as(IComponent)
end
4 archived reader comments
Justin (2006-02-28)
I wonder if there is a reason to have an as operator in Ruby. I’ve only used that operator in C# to give me access to methods on a subclass (for example, when using typed data sets). Why would you need that in Ruby? You get that access for free. I guesss I can see that you might want it when calling a .NET method that expects an object of a certain type, but wouldn’t that be better handled in the bridge?
Also, I wonder why you didn’t overload kindof? and isa? from Kernel instead of creating the is? method?
Regardless, cool work. I’m sure these are things you’ve thought about already and have good reasons for doing the way you did!
Mark (2006-02-28)
Justin, I might venture to explain the need for “as”. In the CLR, you can implement an interface “explicitly”, which means the methods are only available when the object is cast to the interface. Otherwise, the methods are “private”. So, the type can only act like a duck if you’ve asked it to. This is one of those impedance mismatches between Ruby and the CLR.
John Lam (2006-02-28)
Mark’s got it right. Especially since I believe you can have method name collisions across multiple interfaces.
As for why not overloading the Kernel methods - there are two reasons. First, I’m not sure about possible conflicts with behavior, so the safest thing to do was to create new methods. Also, I felt that names like “is” and “as” would feel more natural to .NET developers.
That said, I’m not against overloading the Kernel methods and aliasing the method names, but I’ll need to think some more about that.
Mark (2006/02/28)
Justin, I might venture to explain the need for "as". In the CLR, you can implement an interface "explicitly", which means the methods are only available when the object is cast to the interface. Otherwise, the methods are "private". So, the type can only act like a duck if you’ve asked it to. This is one of those impedance mismatches between Ruby and the CLR.