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

Can't declare static fns in multiple anonymous implementations #4228

Closed
erickt opened this issue Dec 19, 2012 · 7 comments
Closed

Can't declare static fns in multiple anonymous implementations #4228

erickt opened this issue Dec 19, 2012 · 7 comments
Labels
A-trait-system Area: Trait system

Comments

@erickt
Copy link
Contributor

erickt commented Dec 19, 2012

Some static functions may only make sense for certain constrained typarams, but this is disallowed. For example:

type A<T> = ~[T];

impl<T> A<T> {
    static fn new() -> A<T> { ~[] }
}

impl<T: Copy> A<T> {
    static fn new_copyable() -> A<T> { ~[] }
}

fn main() {}

However, rust errors with this message:

test.rs:7:0: 9:1 error: duplicate definition of type A
test.rs:7 impl<T: Copy> A<T> {
test.rs:8     static fn new_copyable() -> A<T> { ~[] }
test.rs:9 }
test.rs:3:0: 5:1 note: first definition of type A here:
test.rs:3 impl<T> A<T> {
test.rs:4     static fn new() -> A<T> { ~[] }
test.rs:5 }
error: aborting due to previous error
@ghost ghost assigned catamorphism Feb 20, 2013
@catamorphism
Copy link
Contributor

Not critical for 0.6 (and possibly a wontfix but I haven't read carefully enough); de-milestoning.

@catamorphism
Copy link
Contributor

I think we're not going to support this.

@erickt erickt reopened this May 24, 2013
@erickt
Copy link
Contributor Author

erickt commented May 24, 2013

@catamorphism said I could reopen this. It's not a priority, but I feel it'd be handy.

@thestinger
Copy link
Contributor

Use case: two constructors for std::rc::{Rc, RcMut}, one for Owned and one for Const. Right now they have to be free functions.

@huonw
Copy link
Member

huonw commented Jun 6, 2013

This would be useful for extra::complex, specifically, to close #5734 by having a separate impl like:

impl<T: Trignometric> Cmplx<T> {
     fn from_polar(r: T, arg: T) -> Cmplx<T> { .. }
     fn to_polar(&self) -> (T,T) { .. }
}

rather than needlessly requiring all types used with Cmplx have sin and cos defined by lumping this into the impl with the generic new.

@brendanzab
Copy link
Member

yeah I hit this too: https://gist.github.com/bjz/7ccf949e65e361863cd2 :(

Use case:

impl<T> Vec4<T> {
    #[inline(always)]
    fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
        Vec4 { x: x, y: y, z: z, w: w }
    }

    #[inline(always)]
    fn len() -> uint { 4 }

    //...
}

impl<T:Copy + Num> Vec4<T> {
    #[inline(always)]
    fn identity() -> Vec4<T> {
        Vec4::new(One::one::<T>(), One::one::<T>(), One::one::<T>(), One::one::<T>())
    }

    #[inline(always)]
    fn zero() -> Vec4<T> {
        Vec4::new(Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>(), Zero::zero::<T>())
    }

    //...
}

Specifically because I would like to be able to to return a Vec4 (like in GLSL) for component-wise vector comparisons.

@luqmana
Copy link
Member

luqmana commented Jun 9, 2013

Fixed by #7029

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system
Projects
None yet
Development

No branches or pull requests

6 participants