3 ms·
Re: function overloading, Rust doesn't support overloaded functions in the same manner as Java or C++, which is important to emphasize to people coming from tho
by kibwen 1y ago
Re: function overloading, Rust doesn't support overloaded functions in the same manner as Java or C++, which is important to emphasize to people coming from those languages.
However, if you then show those people that this is legal Rust:
let addr1 = Ipv4Addr::from([13, 12, 11, 10]);
let addr2 = Ipv4Addr::from(218893066);
...that also runs the risk of confusing them, because this is indistinguishable from function overloading, because it basically is function overloading.
I'm not actually sure what to call this, because unlike Java-style overloading, Rust isn't ad-hoc; everything it's doing is fundamentally integrated into the type system. But unlike strict parametric polymorphism (and like Java), both of the above functions have totally distinct implementations.
- zozbot234 1y ago> I'm not actually sure what to call this I'd call it "Making ad-hoc polymorphism less ad hoc".
- demurgos 1y agoMy understanding is that "ad-hoc" in this context means that you are not defining a single generic implementation but adding individual impls for specific types. There's no fundamental difference between Java's overloading and Rust's implicitly inferred trait implementations in my opinion. Rust's implementation is more orthogonal, so specifying which impl you want explicitly does not require special syntax. It's also based on traits so you'd have to use a non-idiomatic style if you wanted to use overloading as pervasively as in Java. But are those really such big differences? See my reply to the original where I post an example using Rust nightly that is very close to overloading in other languages.
- tavianator 1y agoThey were referencing the title of a specific paper, which invented type classes: https://dl.acm.org/doi/10.1145/75277.75283 https://dl.acm.org/doi/10.1145/75277.75283
- demurgos 1y agoThank you, I did not know this paper and totally missed the reference.
- steveklabnik 1y agoFor sure, that's why I don't think it's the biggest deal. > I'm not actually sure what to call this, Just regular old name resolution. But that's not super satisfying either.