Implement `std::ops` traits to return arbitrary output defined as associated type
- Dominant language
- Rust
- Stars
- 61.4k
- Forks
- 5.4k
- Avg merge
- 3h 33m
- Merged PRs (30d)
- 4
Description
When `std::ops` traits were originally defined, we didn't support associated types. Therefore, all traits, like `Add` or `Sub` were defined as, e.g.:
```sway
pub trait Add {
fn add(self, rhs: Self) -> Self;
}
```
This restricts us in API flexibility. E.g., in `std::time::Time`, adding `Time` and `Duration` requires having `add` on `impl Time`.
The proposal is to migrate to Rust-like traits that have the result type specified as associated type. E.g.:
```sway
pub trait Add {
type Output = Self;
fn add(self, rhs: Rhs) -> Self::Output;
}
```
Before implementing this, we need support for default generic parameters and default associated types, as discussed in https://github.com/FuelLabs/sway/pull/7532#issuecomment-3880005812.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the std::ops trait definitions and the Rust-like design described in the issue, then read the default-generic and default-associated-type discussion in PR #7532. Done means those defaults are supported and the std::ops traits can define an associated Output and accept a distinct RHS, enabling operations such as Time plus Duration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100