-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Comments
This is a subset of GHC's ConstraintKinds extension, and so also related to higher-kinded generics. |
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). |
If you write |
The syntax could easily be something like 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 |
In Haskell the two are indeed equivalent. (Modulo ability to partially apply them which is another higher-kinded generics thing Rust doesn't have.) |
reusing the type keyword for this would be confusing, as it's not a type at all. perhaps we could have a kind keyword. |
It is a type, just of a different kind ;), which is not first-class in Rust. Anyway I think the nicest syntax would be:
(but if generic impl bugs can be worked out there's probably not a pressing need) |
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. |
@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 I'm not sure about using the trait InputNode = AudioNode<OutputBuffer, OutputSample>; |
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 {} |
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? |
Ignore my comments. RFC#1733 is moving in this direction. |
…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
e.g.
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 beimpl
'd separately.The text was updated successfully, but these errors were encountered: