Skip to content

Commit b74894e

Browse files
committed
Allow the Self type to be used in impls.
Closes rust-lang#310
1 parent 44e3043 commit b74894e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

0000-self-impl.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
- Start Date: 2014-12-13
2+
- RFC PR: (leave this empty)
3+
- Rust Issue: (leave this empty)
4+
5+
# Summary
6+
7+
Allow `Self` type to be used in impls.
8+
9+
# Motivation
10+
11+
Allows macros which operate on methods to do more, more easily without having to
12+
rebuild the concrete self type. Macros could use the literal self type like
13+
programmers do, but that requires extra machinery in the macro expansion code
14+
and extra work by the macro author.
15+
16+
Allows easier copy and pasting of method signatures from trait declarations to
17+
implementations.
18+
19+
Is more succinct where the self type is complex.
20+
21+
## Motivation for doing this now
22+
23+
I'm hitting the macro problem in a side project. I wrote and hope to land the
24+
compiler code to make it work, but it is ugly and this is a much nicer solution.
25+
It is also really easy to implement, and since it is just a desugaring, it
26+
should not add any additional complexity to the compiler. Obviously, this should
27+
not block 1.0.
28+
29+
# Detailed design
30+
31+
When used inside an impl, `Self` is desugared during syntactic expansion to the
32+
concrete type being implemented. `Self` can be used anywhere the desugared type
33+
could be used.
34+
35+
# Drawbacks
36+
37+
There are some advantages to being explicit about the self type where it is
38+
possible - clarity and fewer type aliases.
39+
40+
# Alternatives
41+
42+
We could just force authors to use the concrete type as we do currently. This
43+
would require macro expansion code to make available the concrete type (or the
44+
whole impl AST) to macros working on methods. The macro author would then
45+
extract/construct the self type and use it instead of `Self`.
46+
47+
# Unresolved questions
48+
49+
None.

0 commit comments

Comments
 (0)