Newbie question: function overloading

I need to define a method that performs differently when operated on objects
of different type (overloading). Currently I use various if’s to check for
the type of the object as follows:

def somefunction(a)

if a.kind_of?(someType)
expression1
return
end

if a.kind_of(someOtherType)
expression2
return

end

i am wondering if there is a simpler way to implement that without the if’s,
in a way similar to C++ overloading. I mean I would like to define two
functions with the same name that will somehow be aware of the type of
their argument…

sincerely,

DG

I need to define a method that performs differently when operated on objects

of different type (overloading). Currently I use various if’s to check for

the type of the object as follows:

···

On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:

There are really two answers to this:

  1. Ryan Pavlik has created a library that provides overloading capability
    like this. You could give it a try and it might feel right to your C++
    ways.
  2. I would lean more heavily toward this one: The Ruby Way to accomplish
    what you’re trying to accomplish very likely isn’t to check type in this
    way. I remember coming to Ruby from C++/Java and wondering why I couldn’t
    do method overloading like this. My brain was wired into the static
    typing way of doing things and I even felt like overloading was almost
    a necessary part of OO. It didn’t take long before I stopped feeling the
    need for this capability as I started reading existing code and
    understanding the Ruby idioms better.

Maybe if you could post a more specific example/statement of what you’re
trying to accomplish, we could see about helping you with option #2.

Chad

Dimitrios Galanakis wrote:

I need to define a method that performs differently when operated on
objects of different type (overloading). Currently I use various
if’s to check for the type of the object as follows:

def somefunction(a)

if a.kind_of?(someType)
expression1
return
end

if a.kind_of(someOtherType)
expression2
return

end

i am wondering if there is a simpler way to implement that without
the if’s, in a way similar to C++ overloading. I mean I would like
to define two functions with the same name that will somehow be
aware of the type of their argument…

sincerely,

DG

Check out the strongtyping module at RAA:

http://raa.ruby-lang.org/list.rhtml?name=strongtyping

“Dimitrios Galanakis” galanaki@uiuc.edu schrieb im Newsbeitrag
news:4i_eb.147$fm2.69960@vixen.cso.uiuc.edu…

I need to define a method that performs differently when operated on
objects
of different type (overloading). Currently I use various if’s to check
for
the type of the object as follows:

def somefunction(a)

if a.kind_of?(someType)
expression1
return
end

if a.kind_of(someOtherType)
expression2
return

end

i am wondering if there is a simpler way to implement that without the
if’s,
in a way similar to C++ overloading. I mean I would like to define two
functions with the same name that will somehow be aware of the type of
their argument…

You can find further info here:

This might be of interest, too:

Regards

robert

I was expecting an answer like that… I wanted to ask then if operator
overloading if exactly similar to function overloading.
I would like for example to define my own complex class. In this canse
the * operator will perform differencly when multiplying a real and a
complex and when multiplying two complex numbers (this is almost what I
need to do only the difference is that instead of a complex class I have
some other class for which it makes sence to multiply objects of that
class together and with real numbers). In other words how can I simplify
the following code by removing the ifs?

class complex
def (something)
result=self.dup
if something.kind_of?(Numeric)
result.real
=something
result.imag*=something
return result
end
if something.kind_of(complex)
result.real=result.realsomething.real-result.imagsomething.imag
… etc etc etc
end

end

end

Chad Fowler wrote:

···

On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:

I need to define a method that performs differently when operated on objects

of different type (overloading). Currently I use various if’s to check for

the type of the object as follows:

There are really two answers to this:

  1. Ryan Pavlik has created a library that provides overloading capability
    like this. You could give it a try and it might feel right to your C++
    ways.
  2. I would lean more heavily toward this one: The Ruby Way to accomplish
    what you’re trying to accomplish very likely isn’t to check type in this
    way. I remember coming to Ruby from C++/Java and wondering why I couldn’t
    do method overloading like this. My brain was wired into the static
    typing way of doing things and I even felt like overloading was almost
    a necessary part of OO. It didn’t take long before I stopped feeling the
    need for this capability as I started reading existing code and
    understanding the Ruby idioms better.

Maybe if you could post a more specific example/statement of what you’re
trying to accomplish, we could see about helping you with option #2.

Chad

Dimitrios Galanakis wrote:

I was expecting an answer like that… I wanted to ask then if operator
overloading if exactly similar to function overloading.
I would like for example to define my own complex class. In this canse
the * operator will perform differencly when multiplying a real and a
complex and when multiplying two complex numbers (this is almost what I
need to do only the difference is that instead of a complex class I have
some other class for which it makes sence to multiply objects of that
class together and with real numbers). In other words how can I simplify
the following code by removing the ifs?

I’m still a relative Ruby newbie, so I can’t bang out any example code,
but I hope I can point out a couple of useful tidbits. In Ruby classes
aren’t closed. That means you can add methods to Numeric whenever you
want. Also, as written, your code wouldn’t work if you multiplied
Numeric * Complex

I’d make your new class a subclass of Numeric, and then modify the
Numeric class. Alias the old * to something else, and put your own
implementation in place. I still can’t see how to get rid of an ‘if’,
but your new method can check if either element is Complex then call a
complex_multiply method, else use the aliased old *.

-Greg Vaughn

···

class complex
def (something)
result=self.dup
if something.kind_of?(Numeric)
result.real
=something
result.imag*=something
return result
end
if something.kind_of(complex)

result.real=result.realsomething.real-result.imagsomething.imag
… etc etc etc
end

end

end

“Dimitrios Galanakis” wrote:

class together and with real numbers). In other words how can I simplify
the following code by removing the ifs?

Short answer - you can’t - the coerce frame-work is probably your best bet
in a single dispatched world. Expect to see more and more `ifs and elses"
the deeper you extend the Numeric class hierarchy. See

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/51566

for an interesting snippet on Matz’s thinking on the subject.

/Christoph

In this case, what is wanted is Numeric#coerce.

-austin

···

On Fri, 3 Oct 2003 06:14:45 +0900, Greg Vaughn wrote:

Dimitrios Galanakis wrote:

I was expecting an answer like that… I wanted to ask then if operator
overloading if exactly similar to function overloading. I would like
for example to define my own complex class. In this canse the *
operator will perform differencly when multiplying a real and a complex
and when multiplying two complex numbers (this is almost what I need to
do only the difference is that instead of a complex class I have some
other class for which it makes sence to multiply objects of that class
together and with real numbers). In other words how can I simplify the
following code by removing the ifs?
I’m still a relative Ruby newbie, so I can’t bang out any example code,
but I hope I can point out a couple of useful tidbits. In Ruby classes
aren’t closed. That means you can add methods to Numeric whenever you
want. Also, as written, your code wouldn’t work if you multiplied Numeric

  • Complex


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.02
* 18.37.33