> On 15 Nov 2013, at 20:58, Niko Matsakis <n...@alum.mit.edu> wrote:
> 
> On Thu, Nov 14, 2013 at 09:05:40PM +0200, Tommi wrote:
>>> In other words, no autoderef or other transformation takes place.  We
>>> just look for a matching trait. Instead we just pass the values in by
>>> reference. The reason for passing by reference is so that you can
>>> compare linear types more naturally (i.e., `~[1, 2, 3]`).
>> 
>> 
>> I'm sorry, I don't understand that last point. Can you explain why
>> it makes those comparisons more natural.
> 
> Because if you do not pass such types by reference, then they would be 
> consumed
> as part of the comparison. Let me just write out some functions to explain
> what I mean.
> 
> Imagine I had a comparison function for vectors:
> 
>    fn cmp<T>(x: ~[T], y: ~[T]) -> bool {
>        x.len() == y.len() && x.iter().zip(y.iter()).all(|a, b| a == b)
>    }
> 
> Now, when I call this function, it is going to take ownership of its
> arguments:
> 
>    let x = ~[1, 2, 3];
>    let y = ~[4, 5, 6];
>    if cmp(x, y) {
>       use(x); // error
>    }
> 
> This is obviously silly. So the solution is to pass those types by
> reference:
> 
>    fn cmp<T>(x: &~[T], y: &~[T]) -> bool {
>        x.len() == y.len() && x.iter().zip(y.iter()).all(|a, b| a == b)
>    }
>    ...
> 
>    let x = ~[1, 2, 3];
>    let y = ~[4, 5, 6];
>    if cmp(&x, &y) {
>       use(x); // error
>    }
> 
> This is precisely what the binary operators are designed to do. In
> general, the overloadable binary operators are mathematical functions
> of their arguments that are not expected to modify their arguments:
> `==`, `+`, `-`, and so on, so it doesn't make sense for them to take
> ownership of the data they operate on.
> 
> 
> Niko

But shouldn't the signature be:
fn cmp<T>(x: &[T], y: &[T]) -> bool
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to