I need some help.
In RubyCLR, you reference CLR types the same way that you reference Ruby classes:
X::Y::MyGenericType
However, you can overload generic type definitions by arity:
class MyGenericType {}
class MyGenericType<T> {}
class MyGenericType<T, U> {}
Yes, you can mix non-generic type definitions with generic type definitions with the same name.
I’ve been trying really hard to let you just reference a generic type definition, and then create a constructed generic type via the of method:
obj = X::Y::MyGenericType.of(String, Int32).new
The problem arises when you consider that each of the three classes shown above can derive from different base classes:
class MyGenericType : A {}
class MyGenericType<T> : B {}
class MyGenericType<T, U> : C {}
Sure, only a psycho would do this, but it’s permitted by the CLR.
It looks like I have to figure out a way to embed arity metadata into the actual generic type definition type name.[1] I’m leaning towards a solution that looks like:
obj = X::Y::MyGenericType__2.of(String, Int32).new
I’m using two underscores to separate the CLR generic type definition from the arity value.
Does this sound reasonable? Should I use just one underscore? Should I use a different character? Am I going to break some Ruby code in some weird set of corner cases that I’m not considering here?
Thoughts would be appreciated.
[1] Incidentally I always wondered why the TypeName grammar had seemingly redundant arity metadata in the type name – know I know why!
3 archived reader comments
John Lam (2006/08/23)
Thanks for validating that this can be done, Jim! While working in my echo chamber today (aka the local public library) I ran into this problem at the end of a long day of code surgery. I thought it couldn’t be done but I just realized that shadow classes for generic template classes don’t need to derive from another shadow class, only the constructed generic types do. Doh!
Ian Griffiths (2006/08/24)
I’d avoid the underscore syntax if at all possible. It starts to feel very .NET 1.x era Managed C++ish. It’s a technical solution to the problem at hand, but not one you’d want to use every day! I use generic types every day in C#, so I imagine this would be a pain. Then again, maybe there’s less call for using generic types in Ruby.
John Lam (2006/08/24)
I’ve got a build working today that completely avoids the underscore syntax. I’m flushing out a lot of corner cases that didn’t get coverage in the current builds of RubyCLR. I can see the light at the end of the tunnel!