Adding overload to ruby

I don’t know Objective C, but I presume the interfaces are a similar idea as
in Java, i.e. a class full of method definitions but no implementations. Thus
you can say something is “Comparable” because it implements (by contract) the
method “comapre”.

My point: I don’t see how this is missing in Ruby, given the message-sending
instead of method-calling paradigm, and especially given Mixins, which affect
the #is_a? result. (You mix in Enumerable, then your object #is_a?
Enumerable.)

That is, you don’t need to declare something as “Comparable” in order to
compare it. You just send the message “compare” and see what happens.

So that leaves your comment “not in a very straightforward way”. I’d like to
know what you mean, given your stated objection to static typing and anything
that resembles it.

I’m also interested in what you mean and have in mind by “rethinking the
structure of error objects”.

Gavin

···

----- Original Message -----
From: bbense+comp.lang.ruby.Sep.26.02@telemark.stanford.edu

    • 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

Hello bbense+comp,

Friday, September 27, 2002, 12:16:38 AM, you wrote:

bclrS20tse> - - It violates the spirit of Ruby. IMHO, anything that does

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

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

well. say me exactly how many types you personally written which can
be used instead on Integer or String? yaraa don’t have even one

bclrS20tse> - - Some thing that I would like to see more support for is
bclrS20tse> the idea of Interfaces from Objective C.

it’s the same

···


Best regards,
Bulat mailto:bulatz@integ.ru

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:

pigeon% ruby -e 'def a(Array a, Object o) end; def a(Object o, String s) end'
-e:1: Possible ambiguous call [Array, Object] -- [Object, String] (NameError)
pigeon%

Guy Decoux

Hi,

···

In message “Re: adding overload to ruby” on 02/09/27, Mauricio =?unknown-8bit?Q?Fern=E1ndez?= batsman.geo@yahoo.com writes:

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

I like the concept of this idea.

						matz.

Hi,

···

At Fri, 27 Sep 2002 07:00:11 +0900, Mauricio =?unknown-8bit?Q?Fern=E1ndez?= wrote:

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

What about this? :wink:

def do_foo(b {b.respond_to?(:sort) && b.respond_to?(:bar)})
end


Nobu Nakada

In article 006601c265b9$47d98cb0$845186cb@nosedog,

From: bbense+comp.lang.ruby.Sep.26.02@telemark.stanford.edu

    • 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

I don’t know Objective C, but I presume the interfaces are a similar idea as
in Java, i.e. a class full of method definitions but no implementations. Thus
you can say something is “Comparable” because it implements (by contract) the
method “comapre”.

    • I misrememebered the name, it is actually “protocol” that was
      introduced as a compile-time check in Objective C. Basically,
      it was a kind of contract that you would support a certain
      set of method with a given signature. Since Objective-C was
      compiled it’s really not the same problem at all.

My point: I don’t see how this is missing in Ruby, given the message-sending
instead of method-calling paradigm, and especially given Mixins, which affect
the #is_a? result. (You mix in Enumerable, then your object #is_a?
Enumerable.)

That is, you don’t need to declare something as “Comparable” in order to
compare it. You just send the message “compare” and see what happens.

So that leaves your comment “not in a very straightforward way”. I’d like to
know what you mean, given your stated objection to static typing and anything
that resembles it.

    • I retract the “not in a very straighforward way” comment. What
      meant is that if for example you want to have the method
      “print” in a Mixin, it’s a bit awkward to override that method
      in your class. I tend to think of Mixin’s as “meta-methods”
      and a protocol as having more specific methods. Using mixin’s
      implies a specific implementation of a certain method, a
      protocol implies nothing other than that you will respond
      to a messages listed in the protocol.

I’m also interested in what you mean and have in mind by “rethinking the
structure of error objects”.

    • Currently all you get back is an ErrorType and a string. This
      is nice and lightweight, but sometimes the string is well
      “cryptic”. What if for example, when you raise a TypeError
      you returned the Class that you really needed? For example:

    irb(main):001:0> foo = 1
    1
    irb(main):002:0> File.new(foo)
    TypeError: wrong argument type Fixnum (expected String)
    from (irb):2:in initialize' from (irb):2:in new’
    from (irb):2
    irb(main):003:0> begin
    irb(main):004:1* File.new(foo)
    irb(main):005:1> rescue => error
    irb(main):006:1> gerr = error
    irb(main):007:1> end

    • What if gerr.required_class.new gave me an instance of
      the class that I really needed? Or more simply

eval(gerr.required_class).new

    • In this example I can easily parse the error string,
      but in general it’s not this simple since there is no
      standard format for the string ( or even a standard on
      when to use ArgumentError vs TypeError ). If you had
      to supply the type you wanted to use TypeError, that
      would help standardize things quite a bit.
    • Here’s another example

irb(main):001:0> foo = “tempest”
irb(main):002:0> bar = [ 1,2 ]
[1, 2]
irb(main):003:0> File.new(foo,bar)
TypeError: failed to convert Array into String
from (irb):3:in initialize' from (irb):3:in new’
from (irb):3

    • There’s no mapping of the error to the argument
      or the object that caused the error.
    • Booker C. Bense
···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

----- Original Message -----

The average Ruby programmer arguably uses something more than Strings
and Integers.

I have a StringIO that responds to puts, read, write and a bunch of
other methods, which I happily pass in place of IO objects.

In writing code that handles node dependencies in a graph, I used
Hashes as nodes; then switched to a custom class nowhere near Hash in
the class hierarchy. The custom class responds to and not a single
line in the code had to be changed.

The day a `feature’ is added to Ruby that prevents things like these
is the day Ruby loses one developer (at the very least).

IMHO = true

(Which means: imagine a `I think that’ before any sentence below. I
don’t write it just so that my writing keeps a hope of staying
readable.)

Typing is fine in procedural languages, where the thing being typed is
data, a passive and unsurprising entity. But in OO, entities have
behaviours, too.

If you want to check whether someone should be allowed into
House#repair_pipes(plumber), you have some options.

  1. You can check whether the plumber has a Plumber' label printed on his forehead or whether his father was someone with a Plumber’
    label. That’s what Java does. But what if you want your cousin
    in, who happens to be good at repairing pipes? Oh, yeah, you can
    always make both the plumber and your cousin take a certification
    in PlumbingPerson… and of course, years later when a robot will
    come to repair those pipes, you’ll dismantle the whole Plumbers’
    association to reassemble it with PlumbingBeing at the top, and, to
    their immense joy, robots will finally be able to get a `Plumber’
    (or equivalent) label on their metal forehead.

  2. You can ask the plumber: ``Do you know anything about repairing
    pipes?‘’ That’s what one does in Ruby when checking the result of
    plumber.respond_to?(:repair_pipes). Nothing assures you that his
    pipes have to do with tubes and not with tobacco. But nothing
    assures you that the Plumber''s son hasn't become a vandal or the Plumber’ bot hasn’t rebelled to humans, either.

  3. You can just let everyone in and see if the house is still dry when
    they get out. That’s what many do in Ruby, since getting the house
    dry again is a matter of restarting the program.

  4. You can just let everyone in and see if the house is still dry when
    they get out. Then say out loud: ``Computer: holographic
    simulation over!‘’ That’s what people do who do unit testing in
    Ruby.

I’m happy that Ruby doesn’t restrict entrance based on labels printed
on foreheads. I don’t care if more widespread industry-standard
corporate-accepted buzzword-compliant languages do: they’re wrong.
(Hey, remember that IMHO == true!) It all boils down to the question:
do we already have optimal criteria of restricting objects? (I say
no.) If not and must be created from scratch, what would they be
like?

Before (and after) finding the answer, the way I’ll go is unit
testing. I’m sure sooo much feeling of uncertainty would go where it
belongs if only newcomers to Ruby got in touch with unit testing
early.

(Dave… I know you’re busy and that it’s none of my business, but I
can’t help fancying an appendix of the Pickaxe gently introducing the
Ruby-newbie to unit testing…)

Well, as far as quantity of objectionable content is concerned, I
guess I’ve just set a new record among my ruby-talk posts. I hope
I’ll survive the replies.

Massimiliano

···

On Fri, Sep 27, 2002 at 03:04:56PM +0900, Bulat Ziganshin wrote:

bclrS20tse> - - It violates the spirit of Ruby. IMHO, anything that does

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

bclrS20tse> does this as well. It short circuits much of the power of

well. say me exactly how many types you personally written which can
be used instead on Integer or String? yaraa don’t have even one

Hello ts,

Thursday, September 26, 2002, 2:59:20 PM, you wrote:

pigeon% ruby -e ‘def a(Array a, Object o) end; def a(Object o, String s) end’
-e:1: Possible ambiguous call [Array, Object] – [Object, String] (NameError)
pigeon%

why you don’t want to add overload to mainstream ruby?

···


Best regards,
Bulat mailto:bulatz@integ.ru

In article 1033082158.836330.12050.nullmailer@picachu.netlab.jp,

Hi,

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

I like the concept of this idea.

    • You should probably look at the extensions NeXT made to
      Objective C. One of them was the concept of “protocol”,
      Ruby Mixin’s is kind of close, but the idea was that you would
      have a central list of methods that compromised a protocol
      that was independent of any implementation.
    • Protocol’s are supported in recent gnu versions of objective C.
    • I still think multimethod dispatch is evil, but it would be
      a lot less evil if was based on methods rather than types.
    • Booker C. Bense
···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

In message “Re: adding overload to ruby” > on 02/09/27, Mauricio =?unknown-8bit?Q?Fern=E1ndez?= batsman.geo@yahoo.com writes:

again I would write:

def do_foo(b<sort,bar>)
#used when b should respond to sort and bar
end

def do_foo(b)
#used when b should be a type/subsclass of String
end

but I don’t think Ruby needs to use these ‘signatures’, it could just be
better documentation on the methods (and the intent of how the method
will use the parameters).

-rich

···

-----Original Message-----
From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
Sent: Thursday, September 26, 2002 7:16 PM
To: ruby-talk ML
Subject: Re: adding overload to ruby

Hi,

In message “Re: adding overload to ruby” > on 02/09/27, Mauricio =?unknown-8bit?Q?Fern=E1ndez?= > batsman.geo@yahoo.com writes:

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

I like the concept of this idea.

  					matz.

A lighter way to express that is needed, and that’s why I think this
kind of overloading is only syntactic sugar :slight_smile:

···

On Fri, Sep 27, 2002 at 06:35:49PM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 27 Sep 2002 07:00:11 +0900, > Mauricio =?unknown-8bit?Q?Fern=E1ndez?= wrote:

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

What about this? :wink:

def do_foo(b {b.respond_to?(:sort) && b.respond_to?(:bar)})
end


_ _

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

(It is an old Debian tradition to leave at least twice a year …)
– Sven Rudolph

What about this? :wink:

def do_foo(b {b.respond_to?(:sort) && b.respond_to?(:bar)})
end

Why, it's beautiful! Why do we even need this? It is a manual procedure to specify what the parameters must respond to, whereas the interpreter will find out soon enough what it needs to do. (i.e. it will raise error if the object passed in doesn't respond to the methods that are actually used.)

It would perhaps be useful if rdoc could tell us what methods
the parameters need to respond to, but I hardly think it’s an
urgent issue.


Nobu Nakada

Gavin

···

----- Original Message -----
From: nobu.nokada@softhome.net

Nooo… it was very interesting. “Someone” should throw it in the Wiki!

Gavin

···

----- Original Message -----
From: “Massimiliano Mirra” list@NOSPAMchromatic-harp.com

Well, as far as quantity of objectionable content is concerned, I
guess I’ve just set a new record among my ruby-talk posts. I hope
I’ll survive the replies.

Massimiliano

You will for sure. I very much enjoyed your post.

Thanks,
Pit

···

On 29 Sep 2002, at 20:22, Massimiliano Mirra wrote:

(refusal of types in Ruby)

If you want to check whether someone should be allowed into
House#repair_pipes(plumber), you have some options.

(nice allegories of type handling)

Well, as far as quantity of objectionable content is concerned, I
guess I’ve just set a new record among my ruby-talk posts. I hope
I’ll survive the replies.

Hello Massimiliano,

Sunday, September 29, 2002, 3:22:47 PM, you wrote:

well. say me exactly how many types you personally written which can
be used instead on Integer or String? yaraa don’t have even one

The average Ruby programmer arguably uses something more than Strings
and Integers.

I have a StringIO that responds to puts, read, write and a bunch of
other methods, which I happily pass in place of IO objects.

most of current overloading done for string/integer/self.type base

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi –

···

On Thu, 26 Sep 2002, Bulat Ziganshin wrote:

Hello ts,

Thursday, September 26, 2002, 2:59:20 PM, you wrote:

pigeon% ruby -e ‘def a(Array a, Object o) end; def a(Object o, String s) end’
-e:1: Possible ambiguous call [Array, Object] – [Object, String] (NameError)
pigeon%

why you don’t want to add overload to mainstream ruby?

For me, it’s because, as you pointed out, it doesn’t correspond to the
dynamic spirit of the language. There are already languages without
that spirit. I like the fact that Ruby isn’t one of them. I like
iterators and OO regexes and attr_* shortcuts, etc., but I don’t think
that they, alone, are what make Ruby special and deep.

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 24105664687.20020926151354@integ.ru,

Hello ts,

Thursday, September 26, 2002, 2:59:20 PM, you wrote:

pigeon% ruby -e ‘def a(Array a, Object o) end; def a(Object o, String s) end’
-e:1: Possible ambiguous call [Array, Object] – [Object, String] (NameError)
pigeon%

why you don’t want to add overload to mainstream ruby?

    • This is not Ruby. If you really want typed arguements, then you
      should not be using ruby. It isn’t for everybody. This overload
      proposal is nothing more than the camel’s nose of strict typing
      peeking into the tent.
    • One of the fundemental things that makes ruby Ruby is that
      it is not a typed language. This gives the programmer a great
      deal of flexiblity and power, but every freedom comes with a price.
      The price of this flexiblity is giving up the security blanket
      of type checking.
    • Booker C. Bense
···

Bulat Ziganshin bulatz@integ.ru wrote:

Hi,

···

In message “Re: adding overload to ruby” on 02/09/27, bbense+comp.lang.ruby.Sep.26.02@telemark.stanford.edu bbense+comp.lang.ruby.Sep.26.02@telemark.stanford.edu writes:

I like the concept of this idea.

  • You should probably look at the extensions NeXT made to
    Objective C. One of them was the concept of “protocol”,
    Ruby Mixin’s is kind of close, but the idea was that you would
    have a central list of methods that compromised a protocol
    that was independent of any implementation.

  • Protocol’s are supported in recent gnu versions of objective C.

  • I still think multimethod dispatch is evil, but it would be
    a lot less evil if was based on methods rather than types.

I agree with you wholeheartedly.
matz.

Hi,

From: Rich Kilmer [mailto:rich@infoether.com]
Sent: Friday, September 27, 2002 1:31 PM

def do_foo(b<sort,bar>)
#used when b should respond to sort and bar
end

def do_foo(b)
#used when b should be a type/subsclass of String
end

In practice, Ruby’s type cannot be mapped 1-1 to other
language. You have to define Ruby’s language mapping to
IDL or WSDL/XMLSchemaDatatypes and let your script know.
You should right;

def do_foo(b<{http://www.w3.org/2001/XMLSchama}string>)
<{http://www.w3.org/2001/XMLSchema}decimal>
b.length
end

Gee.

ARGS

b {http://www.w3.org/2001/XMLSchama}string

···

RETURN

retVal {http://www.w3.org/2001/XMLSchema}decimal

def do_foo(b)
b.length
end

At any rate, ugly… That’s why I have not written
ruby2wsdl.rb.

Regards,
// NaHi

Hello bbense+comp,

Friday, September 27, 2002, 3:58:44 AM, you wrote:

def do_foo<sort,bar>(b)

used when b can respond to sort and bar

end

I like the concept of this idea.

bclrS20tse> - - You should probably look at the extensions NeXT made to
bclrS20tse> Objective C. One of them was the concept of “protocol”,

how about Eiffel pre and post conditions?

bclrS20tse> - - I still think multimethod dispatch is evil, but it would be
bclrS20tse> a lot less evil if was based on methods rather than types.

method name don’t guarant anything

···


Best regards,
Bulat mailto:bulatz@integ.ru