-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch <>
back to []
#148
Switch <>
back to []
#148
Changes from all commits
e530574
69620ab
af716da
4f9b61a
3a787ff
bbba791
d7dd8ea
ddabf81
f330bdb
c8c3bd2
b493af0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
- Start Date: 2014-06-30 | ||
- RFC PR #: (leave this empty) | ||
- Rust Issue #: (leave this empty) | ||
|
||
# Summary | ||
|
||
Switching (back) the current type parameter syntax from `<>` to `[]`. | ||
|
||
# Motivation | ||
|
||
Recently there has been a lot of talks on simplifying the syntax. Starting from removing the sigils `@` and `~` and making lifetimes less syntax heavy (through various proposals). I think changing the current generic syntax from `<>` to `[]` will make it that much better and clearer (I think `[]` is much easier to read). | ||
|
||
1. `[]` is easier to type than `<>` on *most* keyboards. | ||
|
||
2. IMO `[]` composes **much** better than the more cryptic `<>` form. The `[]` syntax separates the different pieces more so than `<>`. The `<>` syntax elongates everything and it mashes it's contents. This is a common readability issue when working with any nested types (such as encoders and decoders). | ||
|
||
```rust | ||
// Current syntax | ||
fn parse<'a, T: Encodable<Encoder<'a>, IoError>>(value: T) { | ||
// ... | ||
} | ||
``` | ||
|
||
vs | ||
|
||
```rust | ||
// New syntax | ||
fn parse['a, T: Encodable[Encoder['a], IoError]](value: T) { | ||
// ... | ||
} | ||
``` | ||
|
||
3. At the time when Rust switched from `[]` to `<>`, there was no precedence in a C-style language for `[]` generics; this is no longer true: Scala is an example of a language that has become fairly popular recently and which uses `[]` for its generics syntax. | ||
|
||
4. `[]` delimeters are always matching, where one can finally use motions like `%` in Vim (and alternatives in other editors.). | ||
|
||
# Detailed design | ||
|
||
Type parameters would be encapsulated with `[]` instead of `<>`. | ||
|
||
```rust | ||
struct Vec[T] { | ||
// ... | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this bring the ability to have nicer syntax for HKTs? Wouldn't the example be exactly the same when written like this? pub trait Monad<M<T>> {
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is one syntax proposal for HKTs (following in Scala's footsteps) which is ok (There are better alternatives that focus more on inference). I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t see this as a separate point from the second point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define "cryptic to read". The nesting properties of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, imo, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this claim should be removed—it has no substance apart from the previous claim, is subjective and is not dealing with a concrete situation (HKT is far future). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, fixed! |
||
|
||
fn compile['a, T](input: &'a str, arg: T) -> CompiledArg[T] { | ||
// ... | ||
} | ||
``` | ||
|
||
Ambiguities with vector indices are avoided the same way the current syntax works. | ||
|
||
```rust | ||
foo::[int](); | ||
``` | ||
|
||
# Downsides | ||
|
||
* The syntax is used quite a bit. Automation could potentially do some, if not most of the changes (The tricky part is the ambiguities in the current syntax). However, of the changes we've had in the past, I think this syntax change is a whole lot easier to work with than semantic changes, or more complex syntax changes. | ||
|
||
* One that I forgot about is the issue with the indexing syntax, so there might still be ambiguity. | ||
|
||
# Alternatives | ||
|
||
* Keep it like it currently is and end up with the current syntax forever. | ||
|
||
# Unresolved questions | ||
|
||
* Why was did Rust originally have `[]` but decided to switch to `<>`? I heard it was related to try and be consistent with C-class languages (C++, Java, etc...), is this correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By what criteria are you claiming that this "composes much better"? It's literally the the same number of symbols, arranged in the same fashion, with the same basic properties (balanced pairs of some form of bracket), with the only only difference being one uses square brackets and one uses angle brackets.
You seem to be taking it as a given that what you've stated is true, but without providing any evidence whatsoever, I'm unconvinced that this provides a readability benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What precisely do you mean by composes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nesting them (i.e.,
T: Encodable[Encoder['a], IoError]
vsT: Encodable<Encoder<'a>, IoError>
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thehydroimpulse Those nest identically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not the amount of nesting, it's that
[]
separates each piece a lot better (imo) than<>
, resulting in it being easier to read.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thehydroimpulse Then express it that way—for that is different from what people will understand by the other things you’ve said here. Incidentally, I agree with you completely on that claim.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chris-morgan Already in the latest commit.