Here's how you would make that work:

https://gist.github.com/bstrie/5790616

Don't be afraid to ask questions! :)


On Sat, Jun 15, 2013 at 8:06 PM, Rémi Fontan <[email protected]> wrote:

> that's great. thanks.
>
> I tried the following, but unsuccessfully.
>
>
> struct Vec {
>     x:float, y:float
> }
>
> trait VecRhs {
>     fn add_to_Vec(&self, lhs: &Vec) -> Vec;
> }
>
>
> impl Add<Vec, VecRhs> for Vec {
>     fn add(&self, rhs: &VecRhs) -> Vec {
>          rhs.add_to_Vec(self)
>     }
> }
>
> impl VecRhs for float {
>     fn add_to_Vec(&self, lhs: &Vec) -> Vec {
>         Vec{x:lhs.x+*self, y:lhs.x+*self};
>     }
> }
>
> I get the following errors:
> rustc test.rs -o test-test --test
> test.rs:50:14: 50:20 error: reference to trait `VecRhs` where a type is
> expected; try `@VecRhs`, `~VecRhs`, or `&VecRhs`
> test.rs:50 impl Add<Vec, VecRhs> for Vec {
>                          ^~~~~~
>  test.rs:51:4: 53:5 error: method `add` has an incompatible type:
> expected &-ptr but found trait VecRhs
> test.rs:51     fn add(&self, rhs: &VecRhs) -> Vec {
> test.rs:52          rhs.add_to_Vec(self)
> test.rs:53     }
> error: aborting due to 2 previous errors
> make: *** [test-test] Error 101
>
> I feel sorry to ask so many newbies questions. I would really like to go
> past the first learning curve and proceed to my initial project.
>
> thanks for your help.
>
>  Rémi
>
>
>
> On Sun, Jun 16, 2013 at 1:15 AM, Benjamin Striegel <[email protected]
> > wrote:
>
>> This blog post describes how you would achieve this:
>>
>>
>> http://smallcultfollowing.com/babysteps/blog/2012/10/04/refining-traits-slash-impls/
>>
>> See the section entitled "What if I want overloading?"
>>
>>
>> On Sat, Jun 15, 2013 at 5:52 AM, Rémi Fontan <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> I read that rust does not handle function overloading. So I seek your
>>> advise to how I should be handling the following case.
>>>
>>> I have a struct vec2d, which is a 2d vector.  vec2d{x:float, y:float}
>>>
>>> I would like to implement the trait Mul such that I can multiply a
>>> vector by another vector, component by component
>>>
>>> let v1: Vec2d = ...;
>>> let v2: Vec2d = ...;
>>> v1*v2;
>>>
>>> but I also would like to be able to multiply a vec2d with a scalar.
>>> let v3 = v1*-1f;
>>>
>>> I tried implementing the trait Mul twice but the compiler complains. How
>>> would you do it?
>>>
>>> cheers,
>>>
>>> Rémi
>>>
>>>
>>> --
>>> Rémi Fontan : [email protected]
>>> mobile: +64 21 855 351
>>> 93 Otaki Street, Miramar 6022
>>> Wellington, New Zealand
>>>
>>> _______________________________________________
>>> 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
>>
>>
>
>
> --
> Rémi Fontan : [email protected]
> mobile: +64 21 855 351
> 93 Otaki Street, Miramar 6022
> Wellington, New Zealand
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to