[Ironruby-core] Fixnum Patch

Peter Bacon Darwin bacondarwin at googlemail.com
Wed Dec 12 09:56:37 EST 2007

Brian J. Cardiff wrote:

> wouldn't be better to use nullable types? So, for example, it would be

> clear that the result is either null or int, and there is not

> propagation of casts.



This is a fair comment and one that I did consider.


I am generally a bit weary of Nullable types and I didn't feel overly
confident in using them in library code (how often do you actually see them
used in practice?).  This is no excuse of course.


The two reasons in favour of using NullableTypes as I see them are clarity
and performance.



Ruby doesn't really appear to care much about the return type on the method
itself; it is more interested in the object that is returned; and so the
user of the class (in Ruby) won't be able to tell the difference anyway.
There are other cases where object is returned regularly

In fact almost all the Bignum methods return object since the result may
well be converted to a Fixnum (if it is small enough) or even a Float (if
the result is not supposed to be Integer).



In the end there is not much gained in using more specific types in these
cases.  The only reason would be for performance (saving the cost of Boxing
or casting) but to get to the value of a Nullable type you still have to
check whether it is null and then access the Value property anyway so there
is probably not much in it.


public int? Compare(BigInteger self, BigInteger other)

leads to code like

int? result = Compare(someBigInt, otherBigInt);

if (result.HasValue) {

      int actualResult = result.Value

      // Do something with it

} else {

      // Do something with null



public object Compare(BigInteger self, BigInteger other)

leads to code like

object result = Compare(someBigInt, otherBigInt);

if (result is int) {

      int actualResult = (int)result;

      // Do something with it

} else {

      // Do something with null



The main reason I kept away from using them, was that it keeps the options
open and is more in keeping with the general style of the library code.


Am I barking up the wrong tree here?  I may well be missing something.  Let
me know what you think.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20071212/2cfee3b9/attachment.html 

More information about the Ironruby-core mailing list