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

Reply via email to