Rust *does* have function overloading. That's *exactly* what traits are for.
If you want to overload a function, then make it a trait and impl the trait
for all the types you want to overload it with.


On Thu, May 29, 2014 at 4:01 PM, Tommi <[email protected]> wrote:

> Function overloading is a language feature that is necessary in order to
> prevent implementation details from becoming a part of an interface.
>
> Example:
>
> // Assuming function overloading exists...
>
> trait A {
>     // Basic functionality:
>     ...
> }
>
> trait B : A {
>     // Extended functionality:
>     ...
> }
>
> fn foo<T: A>(T t) {
>     // Algorithm 1:
>     ...
> }
>
> fn foo<T: B>(T t) {
>     // Algorithm 2:
>     ...
> }
>
> The fundamental requirement for the Algorithm 1 is that the type of the
> argument `t` must implement trait `A`. But, if the argument `t`implements
> trait `B` (as well as `A`), then the extended functionality in `B` makes
> possible an optimization that the Algorithm 2 is able to use. Whether the
> optimized algorithm is used or not is an implementation detail that the
> caller of `foo` shouldn't need to be bothered with.
>
> The lack of function overloading forces us to have two differently named
> functions, say `foo_a` and `foo_b`, and the programmer has to keep in mind
> that if he wants the optimized algorithm, then he needs to call `foo_b`
> (instead of `foo_a`) if his argument implements `B`. With function
> overloading, the programmer gets the optimization for free (no extra mental
> burden and no chance of calling the "wrong" function).
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to