3 ms·
First of all, don't flame me too much because if you follow my advice it wouldn't be C++ anymore. This is not a suggestion about the post but an invite to think
by pmontra 12y ago
First of all, don't flame me too much because if you follow my advice it wouldn't be C++ anymore. This is not a suggestion about the post but an invite to think.
I always wondered why somebody designs object oriented languages with functions and eventually run into this kind of problems.
An answer might be that they're not willing to wrap fundamental types with classes, even if it would be only syntactic sugar that the compiler can easily unwrap without losing efficiency in the generated code. Probably this is not the only reason, as for the case of Python's len() and the like. Python's designer actually wanted functions to make sure all classes had the same methods, exported as functions.
Furthermore there might be historical reasons, in the case of C++ to make it look like C. Too many differences can scary people away. Think about what it took to make Objective-C successful, the almost impossibility to use any other language to program on a given platform.
Finally, there is convenience, as in void fn(int x) {...} main() { ...; fn(x); ...} By the way, Ruby solves that by adding fn to the Kernel class so it allows the same convenient syntax in a complete OO world.
Anyway, an OO language in which everything is an object doesn't suffer from the kind of problems the post is trying to solve. Years ago I went to Ruby from Java and a little of C++. I was happy not to have that kind of int vs Integer duality. Then I looked at Python and found it a little bit weird with functions mixed to methods, but that's the way it is.
So my advice is as radical as unrealistic in the case of C++: if you want to change the language, make it use methods where it still uses functions. The compiler will deal with that. Remember that when you'll design your next OO language :-)
- bcoates 12y agoIn early-bound, single-dynamic-dispatch languages it makes a small amount of sense, as x.fn() is the only syntax for a dynamic dispatch against object x and fn(x) uses at most compile-time information to determine what fn gets called.