This seems like a way to spell the same thing as the following in Rust, but explicitly attaching the extension to structs and not requiring a trait.
impl<T> Roundable for T where T: Floatable {
fn round(&self) -> isize { ... }
}
I’ve stolen Mojo’s name for float-like things because Rust doesn’t have a standard one.
I think this is a very interesting idea for having reusable non-conforming extensions, especially if we decide that all non-conforming extensions are automatically imported alongside the struct. Rust forbids wildcard (impl<T> T where ... { ... }
) extensions due to the orphan rules, but there’s also the practical concern that, in most cases, extending every single struct is a bad idea.
However, I have an adjacent idea. What if we offered “trait extensions”?
trait extension Floatable:
fn round(self) -> Int:
...
Trait extensions would be required to provide implementations of the behavior, essentially adding a way to add default functions onto a trait using only functions from that trait or other ones which are added as bounds.