Simple question concerning overloading

M

Mathias Weyel

Hello!

I am quite new to Ruby and come directly from the world of Java. While
the lack of a static type system has proven to be somehow irritating to
me, I have stepped over a rather small problem concerning overloading of
methods and parameter names. In Java, this problem cannot occur due to
the static type system.

Here's what I want to do:

I have a class that serves as a container for objects. I want to have a
method "remove" that removes Objects from the list. This method should
exist twice. One which accepts an object and deletes the object that is
== to the given object and another one that accepts ints and deletes the
object at the given index. Here the solution in Java:

class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}

The problem in converting this code to Ruby is that the remove method
has no type and, because of this, cannot be overloaded this way. While
having only one method and checking the type of the given argument
manually seems somehow wrong to me, I wonder, how to call the parameter.
The int parameter I would normally call "index" while the object
parameter would correspond to the objects stored in the list. What name
should the parameter get? I want to have a name that actually makes
sense, but "objectOrIndex" seems stupid.

Greetings

Mathias Weyel
 
G

gabriele renzi

Mathias Weyel ha scritto:
Hello!

I am quite new to Ruby and come directly from the world of Java. While
the lack of a static type system has proven to be somehow irritating to
me, I have stepped over a rather small problem concerning overloading of
methods and parameter names. In Java, this problem cannot occur due to
the static type system.

notice that there are modules (such as the poorly named StrongTyping or
types.rb) that allow you to attach type information to methods, even
allowing overloading. Also there is a module from Florian Gross (IIRC,
ans sorry for not typing the 'b' :) that allows you to use real MMD.

class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}

The problem in converting this code to Ruby is that the remove method
has no type and, because of this, cannot be overloaded this way. While
having only one method and checking the type of the given argument
manually seems somehow wrong to me, I wonder, how to call the parameter.
The int parameter I would normally call "index" while the object
parameter would correspond to the objects stored in the list. What name
should the parameter get? I want to have a name that actually makes
sense, but "objectOrIndex" seems stupid.

why do you feel the need to have a single method?
I mean, they're actually doing differen things, so why not:
class Foo
def remove_object(obj)..end
def remove_index(idx)..end
end

HTH
 
F

Florian Gross

Mathias said:
class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}

This makes no sense in Ruby, because Numbers are also Objects. I'd
suggest naming the method .delete and .delete_at instead.
 
F

Florian Gross

itsme213 said:
Which brings me back to a question I've asked before. In 2.0 with keyword
arguments will

delete (obj)
and
delete(at: index)

be distinguishable methods? If not, I think we will not get the full benefit
of keyword arguments. I'd be forced to use delete_at where I would prefer to
use delete:at:

They will be from inside the method, I suppose. We can build real method
overloading around that easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top