3 ms·
If what you meant was actually a polymorphic function, just write the polymorphic function. If that's not what you meant then stop using the same name. I think
by tialaramex 4y ago
If what you meant was actually a polymorphic function, just write the polymorphic function. If that's not what you meant then stop using the same name.
I think the contrast between string contains in Rust and C++ is illustrative. Overloading means you only get whatever parameter types the stdlib provides in C++.
- int_19h 4y agoFunction overloading is orthogonal to the ability to define additional methods on existing types. C# has both, for example. Overloading is a don't-repeat-yourself thing - if you have a bunch of functions that do fundamentally the same thing with different types, encoding those types in the function name when they're already explicit in the arguments is simply redundant. Then there's an issue with ABI stability. Adding a new function argument breaks any existing compiled code even if there is a default value, but adding a new overload and redirecting the old one to call the new one with the defaults is fine.
- tialaramex 4y ago> if you have a bunch of functions that do fundamentally the same thing with different types That's where you should use parametric polymorphism, that's my point. C++ 23 defines _3_ overloads of string.contains() with parameter defined as variously a char, a char * and a string view, enabling name.contains("Jim") name.contains("Steve"sv) and name.contains('Q'). But if you need a fourth, too bad. Rust doesn't have overloading, so it defines string.contains() as polymorphic over the parameter type Pattern, which enables name.contains("Jim") name.contains('J') name.contains(char::is_uppercase) name.contains(['B', 'o', 'b']) name.contains(|c| { my_fun(c, 206, local_var) }) and so on and so forth. One reason C++ doesn't do this is addressed in Circle, by offering "interfaces" which are approximately equivalent to Rust's traits or the C++ 0x Concepts (as Sean firmly points out, these were very different than Stroustrup's C++ 20 Concepts) The problem is, what does 'Q' have in common with "Jim" ? In conventional C++ polymorphism we want a base class and of course these are basic types, they don't have a base class, much less one which has the necessary commonality. With interfaces, we can say what we care about isn't some hypothetical "base class" of string_view and char, but instead a common interface they both have dedicated to string matching, and now we're writing polymorphic software again.
- seanbax 4y agoYes, interfaces open that up, and to a lesser extent choice types open that up. If your choice type supports all the alternatives from the original overload set, then you can reduce to one function.
- gpderetta 4y agoJust to be precise, traits and overloading are a form ad-hoc polymorphism not parametric.
- tialaramex 4y agoHmm, imagine I invent a new string matching predicate doop() which I want to define. My doop predicate says that the thing we're matching should occur both at the beginning, and the end, and these occurrences should not overlap. That is, there should exist some partition of our string such as that string.doop(pattern) implies all of: s1 + s2 = string and s1.starts_with(pattern) and s2.ends_with(pattern) In C++ I need to overload doop() exactly three times, once for each of the three types we identified. I can't do it any other way, that's how it must be defined. But in Rust I just write it once, in terms of Pattern, I don't need to understand how Pattern is actually implemented, that's opaque to me, I just use this Pattern trait and it all works.
- gpderetta 4y agoIn C++ you would write it once, in term of some concept or other. In practice you would implement exactly the same number of functions you would in rust. edit: to elaborate: some functionality can be implemented generically (i.e. parametrically ) in term of some other concepts, recursively. At some point the concepts need to map to actual concrete implementation, then you use ad hoc polymorphism. This is the same in rust and in c++ [1]. Additionally in C++, even when you can implement some functionality parametrically, it is sometime useful to (partially) specialize functions and types to take advantage of some optimizations (for example it is possible to implement std::copy generically, but it is often specialized for contiguous iterators to trivially copyable types to call memcpy). [1] Rust does of course have a more principled handling of these concepts, which are often underdefined in c++.