4 ms·
But those are excellent examples of the use of generics. They allow you write code that can operates on a generic object, not know what type it is, but know th
by rkangel 9y ago
But those are excellent examples of the use of generics. They allow you write code that can operates on a generic object, not know what type it is, but know that you can read from it.
- saghm 9y agoOh, I think I see what you're saying. If I'm understanding correctly, then you're saying that the types that traits define are only used with generics? This isn't quite correct, as you can use trait objects to get dynamic dispatch rather than using generics. But yes, in general, functions that are defined to take traits as parameters generally use generics. I'm not sure I'd still consider the idea of traits to be inherently tied to generics, though; the Iterator trait, for instance, is usually just used to call Iterator methods on different types rather than as a way to bound the types of function parameters.