Why not set Output type of Add trait to Self by default?

std::ops::Add - Rust (rust-lang.org)

pub trait Add<Rhs = Self> {
    type Output;
    pub fn add(self, rhs: Rhs) -> Self::Output;
}

In Add trait, Rhs type is Self by default, so why not set Output type of Add trait to Self by default?

Because the compiler rejects it:

trait Add<Rhs = Self> {
    type Output = Self;
    fn add(self, rhs: Rhs) -> Self::Output;
}
error[E0658]: associated type defaults are unstable
 --> src/lib.rs:2:5
  |
2 |     type Output = Self;
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #29661 <https://github.com/rust-lang/rust/issues/29661> for more information

error[E0277]: the size for values of type `Self` cannot be known at compilation time
 --> src/lib.rs:2:5
  |
2 |     type Output = Self;
  |     ^^^^^^^^^^^^^^^^^^^
  |     |
  |     doesn't have a size known at compile-time
  |     required by this bound in `Add::Output`
  |
help: consider further restricting `Self`
  |
1 | pub trait Add<Rhs = Self>: Sized {
  |                          ^^^^^^^

error: aborting due to 2 previous errors
4 Likes

It will / is possible for the stdlib to feature this at any point, without requiring associated type defaults, thanks to default / partial impls (weirdly part of the specialization ordeal):

default
impl<T, Rhs> Add<Rhs> for T {
    type Output = T;
}
1 Like