Hacker News new | ask | show | jobs
by robohamburger 3400 days ago
"Idea: implied bounds" sounds like a very interesting idea. It is a pain copying the bounds as author mentions. I also have worked with library code that does not consistently use trait bounds and it can lead to very confusing errors.

The thing that keeps getting me now is there are so many types moving around with generics and traits. It would be nice if it were easier for something to be object safe and/or Any was more powerful. My solution as with many things is to route around it but it is frustrating at times.

3 comments

> Any was more powerful

https://github.com/rust-lang/rfcs/pull/1849 is relevant to your interests; however, as the comments say there, associated type constructors would be needed before it could possibly be used with Any.

Yeah it seems like you would need some kind of guard to make sure you don't violate any of the lifetimes that could be tucked away. Still good work!

I guess compiler plugins and libraries will eventually fill in the gaps, until then I guess I just have to be creative :)

I was surprised that Rust actually did it the way it is now (i.e. no implicit bounds). Article says accessing the map arg's methods will fail, but isn't the entire argument itself invalid by definition? You can't instantiate the hash map without validating that K and V do implement those traits.
In the standard library today, there's no bounds on `K` on the definition of HashMap, only on the impl block containing its methods. This sort of makes HashMap a bad example for this code block; you're right that if the bounds were on the definition instead of the impl, the argument itself would be invalid.
I think it becomes useful only if whatever struct expects some extra level of behavior from the types. Basically any time a struct or a trait is coupled with another trait or needs a particular marker/lifetime to function I find myself repeating the bounds over and over.

For example in gfx-rs it might be repeating the gfx::Resources or gfx::Factory trait bounds.

With implied bounds my gut reaction is that you get c++ templates without "concepts". Ie the situation c++ is in today, where it's perfectly fine to instantiate a template with an implicit bound but you only get a super confusing error message later when you try to use a particular function that this bound requires.