3 ms·
You make it sound like Rust made these choices for the heck of it, whereas there are very good reasons why impl blocks are separate from the struct: 1. Make cl
by dureuill 3y ago
You make it sound like Rust made these choices for the heck of it, whereas there are very good reasons why impl blocks are separate from the struct:
1. Make class methods open to extension. This allows adding methods from other contexts, including other privacy contexts. Sure you could have dedicated syntax like C#'s extension methods, but these were added after the fact. If starting your language from scratch, while have two distinct syntaxes, when one will do?
2. Allows to add methods that only exist for some combinations of generic parameters. For example you can have a generic `Foo<T>` class, and then define a method `from_bar` that only exists for `Foo<Bar>`. Again, impl blocks may not be the only solution to this, but it is a highly cohesive one.
3. Lastly this is more philosophical, but it decouples the data definition from the method definition.
Not wanting to try Rust because its syntax is unfamiliar has to be the weakest reason, especially when the syntax has excellent reasons to be different.