[Ironruby-core] Compare Patch

Tomas Matousek Tomas.Matousek at microsoft.com
Mon Nov 12 17:46:06 EST 2007


For now, use overloads for method specialization on parameter type.
We might do some implicit conversions (e.g. MutableString <-> CLR string) in the binder but such conversions need to be general, not specific to any operator/method.
I'm also thinking about adding support for "overloading by type and protocol" (as opposed to type only). I.e. instead of this

public static void Foo(object obj) {
                if (RubySites.RespondsTo(obj, "bar") { ... 1 ...} else if (RubySites.RespondsTo(obj, "baz") {... 2 ...} else { ... 3 ...}
}

one would just write (declaratively):

public static void FooForBar([HasMethod("bar")]object obj) {
                ... 1 ...
}

public static void FooForBaz([HasMethod("baz")]object obj) {
                ... 2 ...
}

public static void Foo(object obj) {
                ... 3 ...
}

But it's not a priority right now. We'll see.

Tomas

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Peter Bacon Darwin
Sent: Monday, November 12, 2007 5:55 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Compare Patch

I have just realised that the overload I created in FixnumOps is nonsense, sorry:

        [RubyMethod("<=>")]
        public static object Compare(int lhs, int rhs)
        {
            return Compare(lhs, (int)rhs);
        }

        [RubyMethod("<=>")]
        public static object Compare(int lhs, object rhs)
        {
            if (rhs is int)
            {
                return Compare(lhs, (int)rhs);
            }
            return null;
        }

Since there is already an overload that takes int rhs then the second overload will never get called with an int.  Therefore the first three lines of the method are never needed.  That leaves this method looking like:

        [RubyMethod("<=>")]
        public static object Compare(int lhs, object rhs)
        {
            return null;
        }

This then begs the question of is there a better way to pick up default cases like this at binding time?

There is also a need for other overloads such as

        [RubyMethod("<=>")]
        public static int Compare(int lhs, double rhs)
        {
            return Convert.ToDouble(lhs).CompareTo(rhs);
        }

But again is there a more generic method of coercion for these types.  The binder already attempts to do convert parameters but in this case the conversion is the reverse (in effect converting the value of self rather than rhs).
Other implementations talk about a coerce framework but this is offered due to a lack of method overloading based on types in Ruby.  I assume that the way the binding works actually encourages such overloads in IronRuby.

Again sorry if I am being dumb.

Pete



No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.29/1124 - Release Date: 11/11/2007 10:12


No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.29/1124 - Release Date: 11/11/2007 10:12
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20071112/6c945431/attachment-0001.html 


More information about the Ironruby-core mailing list