Adding overload to ruby

Hello all and especially Matz,

there is permanent discussion about adding overload facility to ruby
and spirit of language. i want to add another cent to this :slight_smile:

now i can use overload library and write code such as:

def do_overload(i, s, f)
end
overload(:do_overload, Fixnum, String, Float)

def do_overload(s, i, *a)
end
overload(:do_overload, String, Fixnum)

all that i want - just the same (terrible, you are right) semantics
with a bit more clear syntax and including this feature into ruby
distribution:

def do_overload(Fixnum i, String s, Float f)
end
def do_overload(String s, Fixnum i, *a)
end

although this don’t correspond to dynamic spirit of language, in real
world this adds lot of points to ease of use and even robustness of
language

···


Best regards,
Bulat mailto:bulatz@integ.ru

IMHO, I think that having dispatch like this would be cool. But…

Issues:

  1. It would have to be optional. Each parameter could be either a var (as
    it is now) or a class and var pair to specify specialization.

  2. Making it efficient could be difficult. It could potentially affect the
    speed of all method lookups because a method call would have to get the
    types of the arguments and try and find a matching method. If it can’t find
    one it would presumably use the totally generic case.

    Example:

    def a_method( a, b, c ) #generic case

    end

    def a_method( String a, Fixnum b, Fixnum c ) #specialized case

    end

    a_method( “hello”, 10, 20 ) #calls specialized case above

    a_method( 10, “hello”, “there” ) #can’t find specialized for
    (Fixnum,String,String) so defaults to generic.

  3. Bignum and Fixnum. Conversion between these are automatic. How does
    this affect specialization?

    Example:

    def a_method( Fixnum a )

    end

    def a_method( Bignum a )

    end

    a_method( 1000 ) #calls Fixnum a_method
    a_method( 100000000000000000000000000 ) #calls Bignum a_method

    Is this desirable? Probably not.

···


Justin Johnson

“Bulat Ziganshin” bulatz@integ.ru wrote in message
news:67179315231.20020924111057@integ.ru

Hello all and especially Matz,

there is permanent discussion about adding overload facility to ruby
and spirit of language. i want to add another cent to this :slight_smile:

now i can use overload library and write code such as:

def do_overload(i, s, f)
end
overload(:do_overload, Fixnum, String, Float)

def do_overload(s, i, *a)
end
overload(:do_overload, String, Fixnum)

all that i want - just the same (terrible, you are right) semantics
with a bit more clear syntax and including this feature into ruby
distribution:

def do_overload(Fixnum i, String s, Float f)
end
def do_overload(String s, Fixnum i, *a)
end

although this don’t correspond to dynamic spirit of language, in real
world this adds lot of points to ease of use and even robustness of
language


Best regards,
Bulat mailto:bulatz@integ.ru

Hello Justin,

Wednesday, September 25, 2002, 2:02:37 PM, you wrote:

  1. It would have to be optional. Each parameter could be either a var (as
    it is now) or a class and var pair to specify specialization.

not class, but any expression - we can use === to test

  1. Making it efficient could be difficult. It could potentially affect the
    speed of all method lookups because a method call would have to get the
    types of the arguments and try and find a matching method. If it can’t find
    one it would presumably use the totally generic case.

no. if the method definition don’t use type specifiers, this method is
not overloaded. as with overload.rb

  1. Bignum and Fixnum. Conversion between these are automatic. How does
    this affect specialization?

def a_method( Integer a)
end

def b_method( Numeric a)
end

···

now i can use overload library and write code such as:

def do_overload(i, s, f)
end
overload(:do_overload, Fixnum, String, Float)

def do_overload(s, i, *a)
end
overload(:do_overload, String, Fixnum)

all that i want - just the same (terrible, you are right) semantics
with a bit more clear syntax and including this feature into ruby
distribution:

def do_overload(Fixnum i, String s, Float f)
end
def do_overload(String s, Fixnum i, *a)
end

although this don’t correspond to dynamic spirit of language, in real
world this adds lot of points to ease of use and even robustness of
language


Best regards,
Bulat mailto:bulatz@integ.ru

“Justin Johnson” wrote in

IMHO, I think that having dispatch like this would be cool. But…

Issues:

  1. It would have to be optional. Each parameter could be either a var (as
    it is now) or a class and var pair to specify specialization.

  2. Making it efficient could be difficult. It could potentially affect the
    speed of all method lookups because a method call would have to get the

This is not necessarily true - You could make the
the overridden arguments part explicit. For example
we might write

f(i, am , a, the overridden, part ; rest, of, the, *arguments )

Example:
  1. Bignum and Fixnum. Conversion between these are automatic. How does
    this affect specialization?

As much as I like to argue with Guy over over overloading;-)
I agree with him that mixing automatic conversion and
overloading is a very bad idea (so bad that rather not have
any overloading at all)

/Christoph

not class, but any expression - we can use === to test

Of course. Cool.

def a_method( Integer a)
end

def b_method( Numeric a)
end

Yes, I thought of this. This means however that the lookup needs to work
with class hierarchies:

a_method( 10 )        # parameter is a Fixnum object

Lookup for a_method with a Fixnum find a match. So the call has to navigate
the Fixnum hierarchy to try and find a match. This gets slower/harder with
multiple arguments.

no. if the method definition don’t use type specifiers, this method is
not overloaded. as with overload.rb

Ok, but how does the runtime know wether a method call is overloaded at the
point of call? It doesn’t. It would have to work out the argument classes
and perform a lookup based on them. For most method calls it wouldn’t find
a specialized version and would have to call the generic method instead.

···


Justin Johnson

“Bulat Ziganshin” bulatz@integ.ru wrote in message
news:16124073525.20020925163403@integ.ru

Hello Justin,

Wednesday, September 25, 2002, 2:02:37 PM, you wrote:

  1. It would have to be optional. Each parameter could be either a var
    (as
    it is now) or a class and var pair to specify specialization.

not class, but any expression - we can use === to test

  1. Making it efficient could be difficult. It could potentially
    affect the
    speed of all method lookups because a method call would have to get
    the
    types of the arguments and try and find a matching method. If it
    can’t find
    one it would presumably use the totally generic case.

no. if the method definition don’t use type specifiers, this method is
not overloaded. as with overload.rb

  1. Bignum and Fixnum. Conversion between these are automatic. How
    does
    this affect specialization?

def a_method( Integer a)
end

def b_method( Numeric a)
end

now i can use overload library and write code such as:

def do_overload(i, s, f)
end
overload(:do_overload, Fixnum, String, Float)

def do_overload(s, i, *a)
end
overload(:do_overload, String, Fixnum)

all that i want - just the same (terrible, you are right) semantics
with a bit more clear syntax and including this feature into ruby
distribution:

def do_overload(Fixnum i, String s, Float f)
end
def do_overload(String s, Fixnum i, *a)
end

although this don’t correspond to dynamic spirit of language, in real
world this adds lot of points to ease of use and even robustness of
language


Best regards,
Bulat mailto:bulatz@integ.ru

not class, but any expression - we can use === to test

You have a very strange overload (C++ or j*v* like ???)

This must work

  def a_method(Array a)
  end

  class A < Array
  end

  a_method(A.new)

Guy Decoux

Hello Christoph,

Wednesday, September 25, 2002, 11:23:30 PM, you wrote:

This is not necessarily true - You could make the
the overridden arguments part explicit. For example
we might write

f(i, am , a, the overridden, part ; rest, of, the, *arguments )

no, using at least one type (pattern) specifier will be sign that
function is overloaded. this type can be Object :slight_smile:

  1. Bignum and Fixnum. Conversion between these are automatic. How does
    this affect specialization?

As much as I like to argue with Guy over over overloading;-)
I agree with him that mixing automatic conversion and
overloading is a very bad idea (so bad that rather not have
any overloading at all)

ruby don’t have autoconversion between these two or any other types. i
think, he just say that fixnum+fixnum can be fixnum or bignum, but
it’s not autoconversion. and i never say about autoconversion for
overloaded functions

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hello Justin,

Wednesday, September 25, 2002, 6:02:55 PM, you wrote:

not class, but any expression - we can use === to test

def a_method( Integer a)
end

def b_method( Numeric a)
end

Yes, I thought of this. This means however that the lookup needs to work
with class hierarchies:

blin! i suggest use ‘===’ for all cases

no. if the method definition don’t use type specifiers, this method is
not overloaded. as with overload.rb

Ok, but how does the runtime know wether a method call is overloaded at the
point of call?

in the same way as does overload.rb - auto-creating typeless
dispatcher

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hello ts,

Wednesday, September 25, 2002, 6:28:56 PM, you wrote:

not class, but any expression - we can use === to test

You have a very strange overload (C++ or jv like ???)

prolog-style :slight_smile: pattern-based prototype matching. it’s used in almost
all modern functional languages

This must work

def a_method(Array a)
end

class A < Array
end

a_method(A.new)

it will work. “Array === A.new” evaluates to true

···


Best regards,
Bulat mailto:bulatz@integ.ru

in the same way as does overload.rb - auto-creating typeless
dispatcher

Could you explain this a little more please?

···


Justin Johnson

“Bulat Ziganshin” bulatz@integ.ru wrote in message
news:14683451306.20020926090341@integ.ru

Hello Justin,

Wednesday, September 25, 2002, 6:02:55 PM, you wrote:

not class, but any expression - we can use === to test

def a_method( Integer a)
end

def b_method( Numeric a)
end

Yes, I thought of this. This means however that the lookup needs to
work
with class hierarchies:

blin! i suggest use ‘===’ for all cases

no. if the method definition don’t use type specifiers, this method is
not overloaded. as with overload.rb

Ok, but how does the runtime know wether a method call is overloaded
at the
point of call?

in the same way as does overload.rb - auto-creating typeless
dispatcher


Best regards,
Bulat mailto:bulatz@integ.ru

it will work. "Array === A.new" evaluates to true

Yes, and in the case

   class A < Array; end
   def a_method(Array a) end
   def a_method(A a) end

it's true for the 2

Guy Decoux

Hello Justin,

Thursday, September 26, 2002, 1:44:47 PM, you wrote:

in the same way as does overload.rb - auto-creating typeless
dispatcher

Could you explain this a little more please?

def a(String x)
end
def a(Array y)
end

translated into:

def a1(x)
end
def a2(y)
end
def a(z)
case z
when String then a1(z)
when Array then a2(z)
end
end

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hello ts,

Thursday, September 26, 2002, 2:46:24 PM, you wrote:

it will work. “Array === A.new” evaluates to true

Yes, and in the case

class A < Array; end
def a_method(Array a) end
def a_method(A a) end

it’s true for the 2

of course, it is overloading, not OOO :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

This means that the dispatcher must find the closest match rather than
just using ‘===’.

Doing multi dispatch does seem possible, but Matz seems to think it would be
troublesome. IIRC, other people have shown concerns too.

How would having this feature detriment the language?

···


Justin Johnson

“ts” decoux@moulon.inra.fr wrote in message
news:200209261045.g8QAjRG27127@moulon.inra.fr

it will work. “Array === A.new” evaluates to true

Yes, and in the case

class A < Array; end
def a_method(Array a) end
def a_method(A a) end

it’s true for the 2

Guy Decoux

def a(String x)
end
def a(Array y)
end

translated into:

def a1(x)
end
def a2(y)
end
def a(z)
  case z
  when String then a1(z)
  when Array then a2(z)
  end
end

And this translated to ?

   def a(Array a, Object o) end
   def a(Object o, String s) end

Guy Decoux

of course, it is overloading, not OOO :slight_smile:

Like I've said you use something strange :slight_smile:

Guy Decoux

Hi –

···

On Thu, 26 Sep 2002, Justin Johnson wrote:

This means that the dispatcher must find the closest match rather than
just using ‘===’.

Doing multi dispatch does seem possible, but Matz seems to think it would be
troublesome. IIRC, other people have shown concerns too.

How would having this feature detriment the language?

See the recent thread, starting at [ruby-talk:49988], on “not grasping
the method overloading/multi-dispatch thing”.

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

In article 1033038093.4274.0@iapetus.uk.clara.net,

This means that the dispatcher must find the closest match rather than
just using ‘===’.

Doing multi dispatch does seem possible, but Matz seems to think it would be
troublesome. IIRC, other people have shown concerns too.

How would having this feature detriment the language?

    • It violates the spirit of Ruby. IMHO, anything that does

    raise ArgumentError unless ( foo.type? == String )

    does this as well. It short circuits much of the power of
    the language to allow people to use ideas they are comfortable
    with. It destroys much of the flexibilty of the language.
    If you really think you need those chains then you should
    be using another language.

    • As I stated in a previous post this method overloading is
      nothing but strict type checking in disguise. IHMO, one
      of the whole points of ruby is to get beyond the limitations
      of type checking and start to think in different and more
      powerful ways. You lose much of the inherent power of
      objects when you tie them down with type checking.
    • Some thing that I would like to see more support for is
      the idea of Interfaces from Objective C. This is a kind
      of contract of methods that you support. Modules and
      the kind_of? method sort of support this, but not in
      a very straightforward way. Also, I think much of the
      demand for various kinds of “typed” Ruby could really
      be satisfied by rethinking the structure of error objects.
    • Booker C. bense
···

Justin Johnson justinj@mobiusent.com wrote:

Hello ts,

Thursday, September 26, 2002, 2:51:06 PM, you wrote:

def a(Array a, Object o) end
def a(Object o, String s) end

in this case dispatcher can run any of this functions. in each case we
will get right answer :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

It is IMHO just syntactic sugar (syntactic polymorphism):

def do_foo(String b) def do_foo_str(b)
… …
end end
def do_foo(Array c) def do_foo_arr(b)
… …
end end

do_foo [1, 2, 3, “a”, “b”, “c”] do_foo_arr [1,2,3,“a”,“b”,“c”]
do_foo “Hello” do_foo_str “Hello”

Of course, we want to discriminate on the basis of the supported methods
and not depending on the types. As Ruby is dynamically typed we tend to
think that the type of an object is the set of methods it responds to,
which is why classes don’t really work w/ this kind of overloading.

One foolish way I can think of would be the following:

def do_foo<sort,bar>(b)

used when b can respond to sort and bar

end

def do_foo(b)

when it has method each

end

There’s a lot of reasons why it wouldn’t work. And it’s all sugar,
anyway.

···

On Fri, Sep 27, 2002 at 05:16:38AM +0900, bbense+comp.lang.ruby.Sep.26.02@telemark.stanford.edu wrote:

    • As I stated in a previous post this method overloading is
      nothing but strict type checking in disguise. IHMO, one
      of the whole points of ruby is to get beyond the limitations
      of type checking and start to think in different and more
      powerful ways. You lose much of the inherent power of
      objects when you tie them down with type checking.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

You will not censor me through bug terrorism.
– James Troup