savi
savi copied to clipboard
Extend an existing type
We'd like to be able to extend an existing type to fulfill a trait it didn't fulfill before.
This would be similar to:
- Swift's
extension SomeType: SomeProtocol - Rust's
impl SomeTrait for SomeType
If we allow extending a type to make it implement a trait, there's not much further harm in extending a type for any arbitrary reason, so we might want to consider allowing that as well.
This would be similar to:
- Swift's
extension SomeType - Rust's
impl SomeType(which can occur multiple times, I believe)
Extensions should NOT be allowed to override/replace existing methods/fields, as that would create an order-dependent behavior (where the behavior of which method/field "wins" would be dependent on which order the files were compiled), and would also be likely to cause breakage of dependencies.
Extending a type that belongs to another package should be possible, but discouraged and made auditable in the same way that FFI use is - by clearly marking the extensions in all compile manifests that use that package. If it is not marked in the manifest to allow the extension, the compiler should halt with a compilation error, highlighting the unauthorized extension of an un-owned type.
Rust's impl SomeType can occur multiple times within the same module (i.e. "file"), but it's not something you see very often.
What you see is multiple impl SomeTraitA for SomeType, impl SomeTraitB for SomeType for the same SomeType. Actually traits have to be implemented separately. Also, you cannot partially implement a trait and then fill in the rest of it later on.