![]() Today, attempting a similar pattern with accessors imposes a virtual call on each access.Īnother way this is useful is as as a better encoding of certain patterns to which composition is not well suited. This especially would benefit trit objects, which could implement field access as an offset in the vtable. Our concept of how to solve this use case was to allow traits to define fields (possibly also variants), which their implementers would then ultimately fill in. (This amounts to similar problems to coherence.) Moreover, it runs into thorny compatibility conflicts if two different crates define the same field. Rust structs are not classes, and one shouldn't expect them to behave like so.Īs said, its not possible because we need to know the size and alignment of any struct from the moment it's defined. In the light of this situation, allowing such "partial definitions" would be a huge source of confusion without the added value/motivation of this feature in C#.Īs a closing thought: please don't keep comparing Rust structs to classes in OO languages. know the size of such a struct unless every one of its possible complete implementations are known to the compiler upfront, or using extensive runtime introspection, which Rust doesn't, and shouldn't need to, rely on. in an external crate), and for a good reason: it's pretty much physically impossible to e.g. ![]() ![]() In contrast, it's not possible to add fields to a struct after the fact (e.g. ![]() implementing certain methods only if the generic type arguments satisfy some trait bounds), and it is necessarily true that a single impl block might not contain all the possible behaviors of a type (simply because an impl Trait for Type block might be defined for it even in downstream crates), the ability to do so does not put any more mental pressure on the reader of the code. While the ability to define many impl blocks separately is genuinely necessary for a number of reasons (e.g. Furthermore, I find this redundant at best (for the reasons mentioned by and confusing at worst. "It's useful" is not nearly enough of a justification for a feature to be added. ![]()
0 Comments
Leave a Reply. |