Skip to content
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

Feature Request: Allow default implementations of static methods in derived traits. #3880

Closed
Dretch opened this issue Oct 28, 2012 · 2 comments

Comments

@Dretch
Copy link
Contributor

Dretch commented Oct 28, 2012

I think it would be great if this sort of thing worked:

trait Shizzle {
    static fn shizzle();
}

trait Shozzle : Shizzle {
    static fn shizzle() {
        io::println(~"shizzle! " + shozzle::<self>());
    }
    static fn shozzle() -> ~str;
}

impl int : Shozzle {
    static fn shozzle() -> ~str { ~"i'm an int!" }
}

fn main() {
    shizzle::<int>();
}

Currently it fails with this error:

test-trait-inheritance.rs:12:4: 12:23 error: duplicate definition of value shizzle
test-trait-inheritance.rs:12     static fn shizzle() {
                                 ^~~~~~~~~~~~~~~~~~~
test-trait-inheritance.rs:5:4: 5:23 note: first definition of value shizzle here:
test-trait-inheritance.rs:5     static fn shizzle();
                                ^~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
@Dretch
Copy link
Contributor Author

Dretch commented Oct 28, 2012

Note that a similar pattern using non-static methods works as expected:

trait Shizzle {
    fn shizzle();
}

trait Shozzle : Shizzle {
    fn shizzle() {
        io::println(~"shizzle! " + self.shozzle());
    }
    fn shozzle() -> ~str;
}

impl int : Shozzle {
    fn shozzle() -> ~str { ~"i'm an int!" }
}

fn main() {
    2.shizzle();
}

(this compiles and runs)

@nikomatsakis
Copy link
Contributor

I think this is more a side-effect of the scoping rules for static fns, which promote them to the parent module. If you enclosed Shizzle and Shozzle in different modules, it ought to work (modulo bugs). Those scoping rules are in the process of changing. Therefore, I am going to close this bug.

RalfJung pushed a commit to RalfJung/rust that referenced this issue Sep 17, 2024
add non-portable linux pthread initializers to layout sanity check

Also, turns out we can enable all pthread tests on Solarish.
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