Skip to content

Commit

Permalink
Create version of the array repeat proposal specific to [x; ..N], as …
Browse files Browse the repository at this point in the history
…a backup plan.
  • Loading branch information
quantheory committed Dec 18, 2014
1 parent d4eec1f commit 8103291
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions text/0000-new-array-repeat-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# Summary

Under this RFC, the syntax to specify the type of a fixed-length array
containing `N` elements of type `T` would be changed to `[T; N]`. Similarly, the
syntax to construct an array containing `N` duplicated elements of value `x`
would be changed to `[x; N]`.
containing `N` elements of type `T` would be changed to `[T; ..N]`. Similarly,
the syntax to construct an array containing `N` duplicated elements of value `x`
would be changed to `[x; ..N]`.

# Motivation

Expand All @@ -26,14 +26,14 @@ modifications that would make the range syntax less intuitive.
# Detailed design

The syntax `[T, ..N]` for specifying array types will be replaced by the new
syntax `[T; N]`.
syntax `[T; ..N]`.

In the expression `[x, ..N]`, the `..N` will refer to an expression of type
`RangeTo<T>` (where `T` is the type of `N`). As with any other array of two
elements, `x` will have to be of the same type, and the array expression will be
of type `[RangeTo<T>; 2]`.
of type `[RangeTo<T>; ..2]`.

The expression `[x; N]` will be equivalent to the old meaning of the syntax
The expression `[x; ..N]` will be equivalent to the old meaning of the syntax
`[x, ..N]`. Specifically, it will create an array of length `N`, each element of
which has the value `x`.

Expand All @@ -46,7 +46,7 @@ let a: [uint, ..2] = [0u, ..2];
to this:

```rust
let a: [uint; 2] = [0u; 2];
let a: [uint; ..2] = [0u; ..2];
```

## Match patterns
Expand All @@ -60,7 +60,7 @@ interpreted as a wildcard, and never as sugar for a range constructor.

While not required by this RFC, one suggested transition plan is as follows:

- Implement the new syntax for `[T; N]`/`[x; N]` proposed above.
- Implement the new syntax for `[T; ..N]`/`[x; ..N]` proposed above.

- Issue deprecation warnings for code that uses `[T, ..N]`/`[x, ..N]`, allowing
easier identification of code that needs to be transitioned.
Expand Down Expand Up @@ -108,10 +108,13 @@ be no advocates of this alternative so far.
## Different array repeat syntax

The comments in [pull request #498](https://github.com/rust-lang/rfcs/pull/498)
mentioned many candidates for new syntax other than the `[x; N]` form in this
mentioned many candidates for new syntax other than the `[x; ..N]` form in this
RFC. The comments on the pull request of this RFC mentioned many more.

- Instead of using `[x; N]`, use `[x for N]`.
- Omit the `..`, since it is no longer necessary for disambiguation (a repeat
would therefore be spelled `[x; N]`).

- Instead of using `[x; ..N]`, use `[x for N]`.

- This use of `for` would not be exactly analogous to existing `for` loops,
because those accept an iterator rather than an integer. To a new user,
Expand Down Expand Up @@ -165,7 +168,7 @@ enough to introduce further changes to separate the two. Or this could be
considered innocuous enough to introduce some additional range-related meaning
for `..` in certain patterns.

It is possible that the new syntax `[x; N]` could itself be used within
It is possible that the new syntax `[x; ..N]` could itself be used within
patterns.

This RFC does not attempt to address any of these issues, because the current
Expand Down

0 comments on commit 8103291

Please sign in to comment.