3 ms·
> And with a much better object oriented system based on Traits Note that Traits are a system for generics specifically. You can have useful OO without generic
by rkangel 9y ago
> And with a much better object oriented system based on Traits
Note that Traits are a system for generics specifically. You can have useful OO without generics (C++ is useful without templates, go does't have them at all).
- flavio81 9y agoTraits go a bit beyond generics, in my view. You can say i'm a "smug Lisp coder", but after having used CLOS (Common Lisp Object System), i'd say that generics are one of the most useful things you can have in OOP. With Traits you can separate implementation of certain methods (implementation of Traits) from object definitions. Which for me is excellent, and approaches CLOS' way (in CLOS methods are not directly coupled to a particular object; methods are applied to the combination of one (or more) object(s). In a similar way as English language; where you apply an action involving many subects: "Angel transport his luggage using a car." Here the 'transport' action can't really be bound to the Person class, nor the Luggage class. Perhaps it belongs on the Car class, but with CLOS you can just define "transport" to act on the combination of <actor> <object to be transported> and <vehicle>. "Transport", thus, is a "generic method" and can be defined (implemented) for all kinds of class combinations. With Traits you can achieve something more or less similar, by defining the necessary Traits for each kind of class, to be able to have all of them "transport" harmoniously. So Traits can link (and then impement) the operations performed between classes, without having to bound the code to a particular class.
- lispm 9y agoCLOS: A combination of applicable methods from a generic function is applied to objects.
- saghm 9y agoIn Rust, you can have functions/enums/structs that are generic without traits, and you can have traits that aren't generic. Generics and traits are orthogonal features.
- rkangel 9y agoCan you give me an example of a function, enum or struct that is generic without traits?
- icen 9y agoSomewhat facetiously: fn identity<T>(t: T) -> T { t } Has no trait bounds, and is clearly generic. You typically can't do very much with completely free type variables, but Rust has syntactic support for them.
- Nullabillity 9y agoAnd even that implicitly relies on the Sized trait.
- kibwen 9y agoThe standard library's own `Vec` type is an example of a struct that is generic without traits. See the source: https://github.com/rust-lang/rust/blob/4ed2edaafe82fb8d44e81e00ca3e4f7659855ba2/src/libcollections/vec.rs#L299 https://github.com/rust-lang/rust/blob/4ed2edaafe82fb8d44e81... . In fact, almost no struct or enum uses traits in combination with generics. On the other hand, generic functions do almost always leverage traits, because otherwise you basically can't do anything in the function with the generic parameter. However, note that there does exist a very important generic function in the stdlib that doesn't use traits at all: `std::mem::drop`. Here's the source, which is very, very interesting: https://github.com/rust-lang/rust/blob/4ed2edaafe82fb8d44e81e00ca3e4f7659855ba2/src/libcore/mem.rs#L666 https://github.com/rust-lang/rust/blob/4ed2edaafe82fb8d44e81... . And no, I'm not being facetious when I call it interesting; figuring out what `std::mem::drop`'s purpose is from its amusing implementation is a test that you understand what ownership means in Rust. :)
- rkangel 9y agoThat's the way round I meant. Yes you can have generics without traits (particularly with data structures as you've pointed out). Not sure of anything that uses traits without generics.