[Ironruby-core] Code Review: CompositeConversions3

Curt Hagenlocher curth at microsoft.com
Mon Jan 12 16:11:58 EST 2009

Ruby changes look good.

From: Tomas Matousek
Sent: Friday, January 09, 2009 4:08 PM
To: IronRuby External Code Reviewers; DLR Code Reviews
Cc: ironruby-core at rubyforge.org
Subject: Code Review: CompositeConversions3

tfpt review "/shelveset:CompositeConversions3;REDMOND\tomat"

DLR change (interpreter)

Fixes interpretation of UnaryExpresion cast with a custom method.

Ruby changes

Implements composite conversions and defines default composite protocol conversions. A composite conversion is basically a combination of at least 2 protocol conversions. For example, some methods convert to an integer using "to_int" if available and if not "to_i" is tried. Other methods try "to_str" first and "to_int" then or vice versa to convert to String or Fixnum.

For example, File#new's first parameter performs to_str-to_int conversion. To declare such a parameter a library function uses a parameter of type Union<T1, T2> marked by [DefaultProtocol]:

public static RubyFile/*!*/ CreateFile(RubyClass/*!*/ self,
  [DefaultProtocol]Union<int, MutableString> descriptorOrPath, [Optional, DefaultProtocol]MutableString mode, [Optional]int permission) {

  if (descriptorOrPath.IsFixnum()) { ... } else { ... }

This says that default protocol for Int32 and for MutableString should be performed in the order specified by the type parameters.

Some methods use CastToInteger protocol. This protocol calls to_int, similarly to CastToFixnum, but the result could be both Fixnum or Bignum. This is not a composite conversion since only one conversion is called (to_int) however the type of the result could be either Int32 or BigInteger. There are also other places where we need to pass around a union of Int32 and BigInteger. IntegerValue struct serves this purpose. A default protocol for IntegerValue is CastToInteger. An example of use is Bignum#<<:

public static object/*!*/ LeftShift(RubyContext/*!*/ context, BigInteger/*!*/ self, [DefaultProtocol]IntegerValue other) {
  return other.IsFixnum ? LeftShift(self, other.Fixnum) : LeftShift(self, other.Bignum);

Refactors protocol conversion classes.

Fixes File#open, IO#open, IO#for_fd.
Fixes String#to_i and Kernel#Integer.
Tokenizer now returns whitespace tokens in verbatim mode.

Changes format mode in runrspec to "dotted". It doesn't produce so much noise.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090112/21783814/attachment-0001.html>

More information about the Ironruby-core mailing list