Skip to content

Commit f9e6622

Browse files
committed
rfc, self-in-typedefs: discuss @burdges alternative
1 parent c29d35c commit f9e6622

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

text/0000-self-in-typedefs.md

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,84 @@ it removes exceptional cases especially in the users mental model.
439439
# Rationale and alternatives
440440
[alternatives]: #alternatives
441441

442-
No other designs have been considered. The design choice and rational for
443-
this particular design is straightforward as it would be uneconomic to use
444-
other keywords and also confusing and inconsistent.
445-
446-
The only known alternative to the changes proposed in this RFC is to
447-
simply not implement those changes. However, this has the downsides of not
448-
increasing the ergonomics and keeping the language less consistent than what
449-
it could be. Not improving the ergonomics here may be especially problematic
450-
when dealing with recursive types that have long names and/or many generic
451-
parameters and may encourage developers to use type names which are less
452-
descriptive and keep their code less generic than what is appropriate.
442+
The rationale for this particular design is straightforward as it would be
443+
uneconomic, confusing, and inconsistent to use other keywords.
444+
445+
## Doing nothing
446+
447+
One alternative to the changes proposed in this RFC is to simply not implement
448+
those changes. However, this has the downsides of not increasing the ergonomics
449+
and keeping the language less consistent than what it could be. Not improving
450+
the ergonomics here may be especially problematic when dealing with "recursive"
451+
types that have long names and/or many generic parameters and may encourage
452+
developers to use type names which are less descriptive and keep their code
453+
less generic than what is appropriate.
454+
455+
## Internal scoped type aliases
456+
457+
Another alternative is to allow users to specify type aliases inside type
458+
definitions and use any generic parameters specified in that definition.
459+
An example is:
460+
461+
```rust
462+
enum Tree<T> {
463+
type S = Box<Tree<T>>;
464+
465+
Nil,
466+
Node(T, S, S),
467+
}
468+
```
469+
470+
instead of:
471+
472+
```rust
473+
enum Tree<T> {
474+
Nil,
475+
Node(T, Box<Self>, Box<Self>),
476+
}
477+
```
478+
479+
[generic associated types]: https://github.com/rust-lang/rfcs/blob/master/text/1598-generic_associated_types.md
480+
481+
When dealing with *[generic associated types] (GATs)*, we can then write:
482+
483+
```rust
484+
enum Tree<T, P: PointerFamily> {
485+
type S = P::Pointer<Tree<T>>;
486+
487+
Nil,
488+
Node(T, S, S),
489+
}
490+
```
491+
492+
instead of:
493+
494+
```rust
495+
enum Tree<T, P: PointerFamily> {
496+
Nil,
497+
Node(T, P::Pointer<Tree<T>>, P::Pointer<Tree<T>>),
498+
}
499+
```
500+
501+
As we can see, this approach and alternative is more flexible compared to
502+
what is proposed in this RFC, particularly in the case of GATs. However,
503+
this alternative requires introducing and teaching more concepts compared
504+
to this RFC, which comparatively builds more on what users already know.
505+
Mixing `;` and `,` has also proven to be controversial in the past. The
506+
alternative also opens up questions such as if the type alias should be
507+
permitted before the variants, or after the variants.
508+
509+
For simpler cases such as the first tree-example, using `Self` is also more
510+
readable as it is a special construct that you can easily syntax-highlight
511+
for in a more noticeable way. Further, while there is an expectation from
512+
some users that `Self` already works, as discussed in the [motivation],
513+
the expectation that this alternative already works has not been brought
514+
forth by anyone as far as this RFC's author is aware.
515+
516+
Strictly speaking, this particular alternative is not in conflict with this
517+
RFC in that both can be supported technically. The alternative should be
518+
considered interesting future work, but for now, a more conservative approach
519+
is preferred.
453520

454521
# Unresolved questions
455522
[unresolved]: #unresolved-questions

0 commit comments

Comments
 (0)