# Code Review: CompositeConversions3

**URL:** <https://www.ruby-forum.com/t/code-review-compositeconversions3/157366>\
**Category:** IronRuby\
**Created:** [January 10, 2009, 1:09am UTC](https://www.ruby-forum.com/t/code-review-compositeconversions3/157366 "2009-01-10T01:09:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [January 10, 2009, 1:09am UTC](https://www.ruby-forum.com/t/code-review-compositeconversions3/157366/1 "2009-01-10T01:09:09Z")

</div>

tfpt review “/shelveset:CompositeConversions3;REDMOND\tomat”

DLR change (interpreter)

Fixes interpretation of UnaryExpresion cast with a custom method.

Ruby changes

1. 

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.

1. 

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#\<\<:

[RubyMethod("\<\<")]  
public static object/_!_/ LeftShift(RubyContext/_!_/ context,  
BigInteger/_!_/ self, [DefaultProtocol]IntegerValue other) {  
return other.IsFixnum ? LeftShift(self, other.Fixnum) :  
LeftShift(self, other.Bignum);  
}

1. 

Refactors protocol conversion classes.

1. 

Fixes File#open, IO#open, IO#for\_fd.  
Fixes String#to\_i and Kernel#Integer.  
Tokenizer now returns whitespace tokens in verbatim mode.

1. 

Changes format mode in runrspec to “dotted”. It doesn’t produce so much  
noise.

Tomas

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [January 10, 2009, 2:03am UTC](https://www.ruby-forum.com/t/code-review-compositeconversions3/157366/2 "2009-01-10T02:03:35Z")

</div>

Interpreter change looks good.

From: Tomas M.  
Sent: Friday, January 09, 2009 4:08 PM  
To: IronRuby External Code R.; DLR Code Reviews  
Cc: removed\_email\_address@domain.invalid  
Subject: Code Review: CompositeConversions3

tfpt review “/shelveset:CompositeConversions3;REDMOND\tomat”

DLR change (interpreter)

Fixes interpretation of UnaryExpresion cast with a custom method.

Ruby changes

1. 

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.

1. 

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#\<\<:

[RubyMethod("\<\<")]  
public static object/_!_/ LeftShift(RubyContext/_!_/ context,  
BigInteger/_!_/ self, [DefaultProtocol]IntegerValue other) {  
return other.IsFixnum ? LeftShift(self, other.Fixnum) :  
LeftShift(self, other.Bignum);  
}

1. 

Refactors protocol conversion classes.

1. 

Fixes File#open, IO#open, IO#for\_fd.  
Fixes String#to\_i and Kernel#Integer.  
Tokenizer now returns whitespace tokens in verbatim mode.

1. 

Changes format mode in runrspec to “dotted”. It doesn’t produce so much  
noise.

Tomas

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [January 10, 2009, 2:51am UTC](https://www.ruby-forum.com/t/code-review-compositeconversions3/157366/3 "2009-01-10T02:51:30Z")

</div>

Outer ring looks good.

From: Tomas M.  
Sent: Friday, January 09, 2009 4:08 PM  
To: IronRuby External Code R.; DLR Code Reviews  
Cc: removed\_email\_address@domain.invalid  
Subject: Code Review: CompositeConversions3

tfpt review “/shelveset:CompositeConversions3;REDMOND\tomat”

DLR change (interpreter)

Fixes interpretation of UnaryExpresion cast with a custom method.

Ruby changes

1. 

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.

1. 

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#\<\<:

[RubyMethod("\<\<")]  
public static object/_!_/ LeftShift(RubyContext/_!_/ context,  
BigInteger/_!_/ self, [DefaultProtocol]IntegerValue other) {  
return other.IsFixnum ? LeftShift(self, other.Fixnum) :  
LeftShift(self, other.Bignum);  
}

1. 

Refactors protocol conversion classes.

1. 

Fixes File#open, IO#open, IO#for\_fd.  
Fixes String#to\_i and Kernel#Integer.  
Tokenizer now returns whitespace tokens in verbatim mode.

1. 

Changes format mode in runrspec to “dotted”. It doesn’t produce so much  
noise.

Tomas

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [January 12, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/code-review-compositeconversions3/157366/4 "2009-01-12T22:29:15Z")

</div>

Ruby changes look good.

From: Tomas M.  
Sent: Friday, January 09, 2009 4:08 PM  
To: IronRuby External Code R.; DLR Code Reviews  
Cc: removed\_email\_address@domain.invalid  
Subject: Code Review: CompositeConversions3

tfpt review “/shelveset:CompositeConversions3;REDMOND\tomat”

DLR change (interpreter)

Fixes interpretation of UnaryExpresion cast with a custom method.

Ruby changes

1. 

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.

1. 

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#\<\<:

[RubyMethod("\<\<")]  
public static object/_!_/ LeftShift(RubyContext/_!_/ context,  
BigInteger/_!_/ self, [DefaultProtocol]IntegerValue other) {  
return other.IsFixnum ? LeftShift(self, other.Fixnum) :  
LeftShift(self, other.Bignum);  
}

1. 

Refactors protocol conversion classes.

1. 

Fixes File#open, IO#open, IO#for\_fd.  
Fixes String#to\_i and Kernel#Integer.  
Tokenizer now returns whitespace tokens in verbatim mode.

1. 

Changes format mode in runrspec to “dotted”. It doesn’t produce so much  
noise.

Tomas
