Skip to content

Why not use this "Circle :: new ()" #14770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
electricface opened this issue Jun 9, 2014 · 1 comment
Closed

Why not use this "Circle :: new ()" #14770

electricface opened this issue Jun 9, 2014 · 1 comment

Comments

@electricface
Copy link

use std::f64::consts::PI;
trait Shape { fn new(area: f64) -> Self; }
struct Circle { radius: f64 }
struct Square { length: f64 }

impl Shape for Circle {
    fn new(area: f64) -> Circle { Circle { radius: (area / PI).sqrt() } }
}
impl Shape for Square {
    fn new(area: f64) -> Square { Square { length: area.sqrt() } }
}

let area = 42.5;
let c: Circle = Shape::new(area);
let s: Square = Shape::new(area);

Why is this wrong

    let c = Circle::new( area );
    let s = Square::new( area );
traits-test.rs:126:10: 126:21 error: unresolved name
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:126:10: 126:21 error: use of undeclared module `Circle`
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:126:10: 126:21 error: unresolved name `Circle::new`.
traits-test.rs:126  let c = Circle::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: unresolved name
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: use of undeclared module `Square`
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
traits-test.rs:127:10: 127:21 error: unresolved name `Square::new`.
traits-test.rs:127  let s = Square::new( area );
                            ^~~~~~~~~~~
error: aborting due to 6 previous errors
@huonw
Copy link
Member

huonw commented Jun 9, 2014

Trait methods are always called via the trait; we just don't have support for anything else yet.

Dupe of #6894. (Thanks for filing!)

@huonw huonw closed this as completed Jun 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants