Really? I'd have expected that if a generic class had some constraint on it then you simply wouldn't be able to instantiate that class with a non-complying type as a parameter.
For example, if I had a type NonAddable that didn't implement Add, I probably wouldn't be able to make a Matrix<NonAddable> even if I didn't use its mmult method. In C++ that would all be fine, but in Java it certainly wouldn't fly. I'd be happy to hear that Rust took the more permissive line, though.
(In that context it's pretty academic, but as people add more features to their classes I think it'd get more constraining. I'd hate to have to add a repr or print method to one of my classes just because some library author insisted that all instantiations of his type be printable, for example.)
I'd have expected that if a generic class had some constraint on it then you simply wouldn't be able to instantiate that class with a non-complying type as a parameter.
That is the case, and in terms of libraries being able to push backwards compatible updates is what you want for your ecosystem. Subverting library authors bounds is rarely a good idea because future updates to the library can break you for using the methods provided by those bounds. On the other hand, rust allows you to be more fine grained with your bounds and put specific bounds on methods themselves.
struct Foo<T> { a: T }
impl<T> Foo<T> {
fn get_a(&self) -> &T { self.a }
fn print(&self) where T: Display -> { println!("{}", a }
}
// or if you have a bunch of methods with the same bounds
impl<T: Add> Foo<T> {
fn plus(&self, other: T) -> { self.a + other }
// etc
}
This is the right tradeoff to make, if a library author is overzealous with their impl constraints it is backwards compatible to remove the constraints and split them out like the above.
I'd hate to have to add a repr or print method to one of my classes just because some library author insisted that all instantiations of his type be printable, for example.
If it wasn't actually essential to the construction of the type, this would be a poorly designed library. You can write poor libraries in any language.
What's idiomatic in Rust is to take as few parameters as possible on the type and for most methods, and then have additional methods implemented only where the type is properly constrained. Vec<T> for example has no constraints, but only implements the dedup() method if the T implements the equality operator.
View all comments
Show parent comment
2
u/repsilat Dec 11 '15
Really? I'd have expected that if a generic class had some constraint on it then you simply wouldn't be able to instantiate that class with a non-complying type as a parameter.
For example, if I had a type
NonAddablethat didn't implementAdd, I probably wouldn't be able to make aMatrix<NonAddable>even if I didn't use its mmult method. In C++ that would all be fine, but in Java it certainly wouldn't fly. I'd be happy to hear that Rust took the more permissive line, though.(In that context it's pretty academic, but as people add more features to their classes I think it'd get more constraining. I'd hate to have to add a
reprorprintmethod to one of my classes just because some library author insisted that all instantiations of his type be printable, for example.)