Created
February 20, 2014 07:45
-
-
Save cceckman/9108720 to your computer and use it in GitHub Desktop.
Trait inheritance in Rust?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gist.rs:12:5: 16:6 error: method `add` is not a member of trait `vector<A>` | |
| gist.rs:12 fn add(&self, rhs : &(A,A,A)) -> (A,A,A){ | |
| gist.rs:13 let (x,y,z) = *self; | |
| gist.rs:14 let (a,b,c) = *rhs; | |
| gist.rs:15 (x+a, y+b, c+z) | |
| gist.rs:16 } | |
| gist.rs:11:1: 17:2 error: failed to find an implementation of trait std::ops::Add<(A,A,A),(A,A,A)> for (A,A,A) | |
| gist.rs:11 impl<A : Num> vector<A> for (A, A, A){ | |
| gist.rs:12 fn add(&self, rhs : &(A,A,A)) -> (A,A,A){ | |
| gist.rs:13 let (x,y,z) = *self; | |
| gist.rs:14 let (a,b,c) = *rhs; | |
| gist.rs:15 (x+a, y+b, c+z) | |
| gist.rs:16 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::num; | |
| use std::ops; | |
| use std::fmt; | |
| trait vector<A : Num> : | |
| Add<Self, Self> | |
| { } | |
| // points in 3-space | |
| impl<A : Num> vector<A> for (A, A, A){ | |
| fn add(&self, rhs : &(A,A,A)) -> (A,A,A){ | |
| let (x,y,z) = *self; | |
| let (a,b,c) = *rhs; | |
| (x+a, y+b, c+z) | |
| } | |
| } | |
| pub fn main() -> int{ | |
| let x = (3.0, 3.0, 3.0); | |
| let y = (1.0, 1.5, 1.0); | |
| let z = x + y; | |
| let s = format!("Hi, rust! z is ({}, {}, {})", z.n0(), z.n1(), z.n2()); | |
| std::io::stdio::println(s); | |
| 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiling gist.rs with rustc gives output in error.log. I fail to see how vector<A> could require implementing std::ops::Add and yet be told that it doesn't meet the signature. I'm guessing I've got the add signature wrong- but how?