[Ironruby-core] Fixnum Patch

Peter Bacon Darwin bacondarwin at googlemail.com
Fri Dec 14 15:25:54 EST 2007

As it turns out, since there is loads of boxing going on in IronRuby/DLR you
do actually get the right semantics for free.

When a nullable value type is boxed you either get a null reference or a
boxed version of the underlying value type.  Great!  This means that, since
all dynamic invocations go via methods that look like this (note the
"object" return type):

	public override object Invoke(object arg0) { return _target(arg0 !=
null ? (T0)arg0 : default(T0)); }

The nullable type gets converted into a regular null or boxed value type.
You can see this happen if you step through the code.  It is a bit creepy
actually.  The DLR knows that the return type of a method is actually a
Nullable<Int32> but the actual value is either a null or Int32.

The example given below, therefore, does actually do what you want it to.
The value of x actually is a Fixnum and so works like a treat.

This is still all a bit scary and I personally think keeping object rather
than int? is safer and doesn't detract from the clarity.  Also, since there
is all this boxing going on anyway you get no performance benefit either.


John Messerly wrote:

>> On second thought, it might be better to not use Nullable<int>. As Dino &
Tomas pointed out, now we're playing games with our
>> type system, because to the CLR Int32 and Nullable<Int32> are different
types. For example, if you have a CLR method "foo" that
>> returns Nullable<Int32>:
>> x = obj.foo    # x is now a Nullable<Int32>
>> 5 + x          # this is probably okay; the method binder converts
Nullable<Int32> arguments to Int32
>> x + 5          # how does this work? Nullable<T> doesn't define the Ruby
method "+"
>> Since x is a Nullable<Int32>, we should be looking up method calls on
Nullable<Int32>. But that won't work...
>> Actually, it might be a bug that it's working now. Probably better to
just leave return values as "object".
>> - John

More information about the Ironruby-core mailing list