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

Local type aliases to concrete types #1697

Open
ebfull opened this issue Aug 2, 2016 · 5 comments
Open

Local type aliases to concrete types #1697

ebfull opened this issue Aug 2, 2016 · 5 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@ebfull
Copy link

ebfull commented Aug 2, 2016

struct Foo<T>(T);

fn test<T, U>() {
    type A = Foo<T>;
    type B = Foo<U>;

    let a = A::method();
    let b = B::method();

    // and so on...
}

This is not possible because of an error: can't use type parameters from outer function; try using a local type parameter instead.

There are probably good reasons for this error but I really think there should be a way to accomplish this kind of aliasing. Otherwise it makes some code noisy and hard to read:

let a = Foo::<T>::method();
let b = Foo::<U>::method();

...especially when you use Foo<T> and Foo<U> a lot.

@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label Aug 4, 2016
@gsingh93
Copy link

There's a similar problem for impls. For example, I'd like this code to compile:

use std::marker::PhantomData;

struct Foo<Bar> {
    foo: PhantomData<Bar>,    
}

impl<Bar> Foo<Bar> {
    type Qux = Vec<Bar>;

    fn baz() -> Qux {
        vec![]
    }
}

But I get associated types are not allowed in inherent impls. I'm thinking we'd have to make a new keyword for type aliases to allow this.

@burdges
Copy link

burdges commented Jan 15, 2017

Associated constants would be nice in both fn bodies and inherent impls too. I'd think structs could benefit from all three as well.

@withoutboats
Copy link
Contributor

I'm thinking we'd have to make a new keyword for type aliases to allow this.

No, we just have to make associated types allowed in inherent impls. This is actually a part of the RFC which introduced the concept IIRC.

@eddyb
Copy link
Member

eddyb commented Jan 16, 2017

On the implementation side this has been hard because looking up associated types happens very early on, before a coherent list of impls is even built. We should be able to address this Soon (TM).

@Centril
Copy link
Contributor

Centril commented Apr 26, 2018

This is essentially ScopedTypeVariables afaik.

@eddyb are we there yet? (re. Soon (TM))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

7 participants