Pre-RFC: allow `Self` in impls

How do people feel about this?

In an impl, Self would be allowed and would be an alias to the concrete type. E.g.,

impl Foo for int {
    fn f(x: Self) -> Self { ... }
}

Both uses of Self would be equivalent to using int.

The motivation is for macros. I would like to allow procedural macros to modify methods as well as functions, but you often need to use the concrete type for this. To do so you either need to pass the concrete type to the macro (which I find a bit hackey) or allow Self to be used in impls. This option definitely makes making the macros and implementing the macro expansion in compiler easier. I’m not really sure if it is a good thing or a bad thing for the language.

2 Likes

Yes please. Even outside macros, it’s nice sugar when the type is lengthy (e.g. has many parameters). It also seems low-impact.

It also enables copy-pasting of a trait’s signature, which is nice.

I think it’s useful and consistent with other parts of the language - i.e traits. I like the idea.

https://github.com/rust-lang/rfcs/issues/310

edit: oh yeah, +1

Since this seems to be popular here and in the previous thread (thanks for the link @jfager!), I made an RFC: https://github.com/rust-lang/rfcs/pull/522.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.