views:

172

answers:

5

Ok - this may be a very stupid question, but it's been bothering me.

Is there a language where

class Animal;
class Ape : public Animal
{...}

void doStuff(Animal* animalPtr)
{
    cout << "doing animal stuff" << endl;
}

void doStuff(Ape* apePtr)
{
    cout << "doing ape stuff" << endl;
}

Animal *ape = new Ape();
doStuff(ape);

would yield "doing ape stuff"? (please bear with me using C++ syntax) To clarify, I want "a function that accepts an argument and acts on it according to the type of the argument".

And would it make sense? Of course, as a developer you'd need to take care since instances that look just like an Animal pointer might actually call Ape code, because at runtime it's an Ape instance being pointed to.

+1  A: 

Potentially, but it would not be C++ since the function overloading lookups in C++ are done at compile-time, not runtime as would be required here. It would require a dynamic language that allows type-hinting and overloading.

Ignacio Vazquez-Abrams
Well, I know that C++ does not support the feature I am "sorta" looking for. Thought just came to me when coding in C++.Is this what make the difference between dynamic / static languages?
msiemeri
Yes. Dynamic languages do their type checks/attribute lookups at runtime instead of compile-time. It slows down their operation a bit, but allows for greater flexibility.
Ignacio Vazquez-Abrams
No no no - you can have a very dynamic language (say Python) which does not support this out of the box... you have to use a special "multimethods" addon.
Steven Schlansker
C++ *could* support lookup on function parameters. It does not, partly because of performance reasons.
ebo
@Steven: Python supports neither type-hinting nor overloading, so I am not surprised.
Ignacio Vazquez-Abrams
+1  A: 

There is some inconsistency here that is confusing. Do you want a function that accepts an argument and acts on it according to the type of the argument? This wouldn't really be polymorphism as the functions are on their lonesome, they aren't methods belonging to a class or interface hierarchy. In other words, its kindof like mixing OO paradigms with procedural paradigms.

If it is the type you want to parameterize and not the variable, you would use something like Java Generics. With generics you can inform a method that the type of the parameter coming in will also be variable. The method acts on the variable type in a generic fashion.

darren
Nice description of the feature I am searching for - I was so bold to put it in an edit of my issue. You're right - polymorphism probably is a bad term, but none other came across my mind.
msiemeri
+4  A: 

Yes, there are! This is called multiple dispatch. The Wikipedia article is very good. Sadly, it seem to only be supported via language extensions for most popular languages, but there are a few (mostly esoteric) languages which support it natively.

Steven Schlansker
Common LISP... Why that's a surprise (not!) ;)Thank you for term clarification and the Wikipedia link!
msiemeri
I'll second common lisp.
Demosthenex
It's almost sad to have to classify it as esoteric :-p
Steven Schlansker
+2  A: 

Open Multi-Methods for C++, by Peter Pirkelbauer, Yuriy Solodkyy, and Bjarne Stroustrup.

This paper discusses a language extention to integrate multi-methods into C++ along with some very interesting implementation details, such as dealing with dynamically loaded libraries and the actual layout for dispatching properly. Note that it is not yet part of the C++ standard, and probably not a part of any major compiler.

MSN
+5  A: 

Take a look at the Visitor pattern

mloskot
For languages that don't support multiple dispatch and/or pattern matching Visitor pattern is probably the best solution.
Evan