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

Allow type to create an alias for several trait bounds #8634

Closed
huonw opened this issue Aug 20, 2013 · 13 comments
Closed

Allow type to create an alias for several trait bounds #8634

huonw opened this issue Aug 20, 2013 · 13 comments
Labels
A-traits Area: Trait system A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@huonw
Copy link
Member

huonw commented Aug 20, 2013

e.g.

trait Foo {}
trait Bar {}


type Alias = Foo + Bar;

// typechecks fine:

fn bar<T: Foo + Bar>(t: T) {
    baz(t)
} 
fn baz<T: Alias>(t: T) {
    bar(t)
}

This is similar to defining a trait inheriting from both (trait Alias: Foo + Bar {}) but it's purely a synonym and doesn't have to be impl'd separately.

@glaebhoerl
Copy link
Contributor

This is a subset of GHC's ConstraintKinds extension, and so also related to higher-kinded generics.

@huonw
Copy link
Member Author

huonw commented Aug 21, 2013

It is, but only very weakly; the proposal here is literally just a synonym to make code shorter/nicer, so this could even be implemented via textual substitution, modulo scoping concerns (although this obviously shouldn't be the actual implementation).

@glaebhoerl
Copy link
Contributor

If you write type Foo = Bar I think you'd need some kind of kind inference at some point to figure out if it's a type or a trait.

@huonw
Copy link
Member Author

huonw commented Aug 21, 2013

The syntax could easily be something like trait type Foo = Bar. Thinking about it now, I'm not sure how different this would be from:

trait FooBar: Foo + Bar {}
impl<T: Foo + Bar> FooBar for T {}

other than the fact that these generic impls are slightly buggy. I guess a macro that expands to the above (assuming that pattern works) would be a solution.

(And the kind "inference" could just be checking that Foo is valid at the place it is used at those places.)

@glaebhoerl
Copy link
Contributor

In Haskell the two are indeed equivalent. (Modulo ability to partially apply them which is another higher-kinded generics thing Rust doesn't have.)

@bblum
Copy link
Contributor

bblum commented Aug 22, 2013

reusing the type keyword for this would be confusing, as it's not a type at all. perhaps we could have a kind keyword.

@glaebhoerl
Copy link
Contributor

It is a type, just of a different kind ;), which is not first-class in Rust. Anyway I think the nicest syntax would be:

trait FooBar = Foo + Bar

(but if generic impl bugs can be worked out there's probably not a pressing need)

@catamorphism
Copy link
Contributor

I don't see a compelling need for this, since you can just declare another empty trait that inherits from both. I realize it's somewhat more concise if you have the additional syntax, but it just doesn't seem to add enough value to justify adding a new feature. Closing.

@mitchmindtree
Copy link
Contributor

@catamorphism I think this would be handy for traits that have a number of type parameters i.e.

type InputNode = AudioNode<OutputBuffer, OutputSample>;

With this I imagine you could write

impl InputNode for Synth {}

whereas simply deriving from AudioNode<OutputBuffer, OutputSample> would require you to impl both InputNode and AudioNode<OutputBuffer, OutputSample>, right?

I'm not sure about using the type keyword though. Could we just use trait? i.e.

trait InputNode = AudioNode<OutputBuffer, OutputSample>;

@emk
Copy link
Contributor

emk commented Dec 12, 2014

I also would love to be able to write:

pub trait DuktapeEncodable = Encodable<Encoder, DuktapeError>;

The workaround is simple enough, fortunately:

pub trait DuktapeEncodable: Encodable<Encoder, DuktapeError> {}
impl<T: Encodable<Encoder, DuktapeError>> DuktapeEncodable for T {}

@jonysy
Copy link

jonysy commented Mar 21, 2017

Similar to @emk's case, I'd love to be able to write:

pub trait Blas = Vector<f32> + Vector<f64> + MatrixVector<f32> ...

instead of:

pub trait Blas: Vector<f32> + Vector<f64> + MatrixVector<f32> ... { }

impl<I: Vector<f32> + Vector<f64> + MatrixVector<f32> ...> Blas for I { } 

The first example is clear. The second, not so much.. @catamorphism will you consider reopening this issue?

@jonysy
Copy link

jonysy commented Mar 21, 2017

@jonysy
Copy link

jonysy commented Mar 21, 2017

Ignore my comments. RFC#1733 is moving in this direction.

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2023
…dnet

[`single_match`]: don't lint if block contains comments

Fixes rust-lang#8634

It now ignores matches with a comment in the "else" arm

changelog: [`single_match`]: don't lint if block contains comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

7 participants