Skip to content

Commit 29b098e

Browse files
committed
Back out a[begin] syntax
It was decided to delay this syntax change until after 1.0, since it is non-breaking. Once somebody wants to take this up again, they can simply revert this commit.
1 parent 20f7543 commit 29b098e

File tree

5 files changed

+21
-40
lines changed

5 files changed

+21
-40
lines changed

NEWS.md

-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ New language features
4444
* Values for `Enum`s can now be specified inside of a `begin` block when using the
4545
`@enum` macro ([#25424]).
4646

47-
* `a[begin]` can now be used to address the first element of an integer-indexed collection `a`.
48-
The index is computed by `firstindex(a)` ([#23554]).
49-
5047
Language changes
5148
----------------
5249

doc/src/manual/functions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,8 @@ the results (see [Pre-allocating outputs](@ref)). A convenient syntax for this i
692692
is equivalent to `broadcast!(identity, X, ...)` except that, as above, the `broadcast!` loop is
693693
fused with any nested "dot" calls. For example, `X .= sin.(Y)` is equivalent to `broadcast!(sin, X, Y)`,
694694
overwriting `X` with `sin.(Y)` in-place. If the left-hand side is an array-indexing expression,
695-
e.g. `X[begin+1:end] .= sin.(Y)`, then it translates to `broadcast!` on a `view`, e.g.
696-
`broadcast!(sin, view(X, firstindex(X)+1:lastindex(X)), Y)`,
695+
e.g. `X[2:end] .= sin.(Y)`, then it translates to `broadcast!` on a `view`, e.g.
696+
`broadcast!(sin, view(X, 2:lastindex(X)), Y)`,
697697
so that the left-hand side is updated in-place.
698698

699699
Since adding dots to many operations and function calls in an expression

doc/src/manual/interfaces.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ julia> collect(Iterators.reverse(Squares(10)))' # transposed to save space
161161
|:-------------------- |:-------------------------------- |
162162
| `getindex(X, i)` | `X[i]`, indexed element access |
163163
| `setindex!(X, v, i)` | `X[i] = v`, indexed assignment |
164-
| `firstindex(X)` | The first index, used in `X[begin]` |
165-
| `lastindex(X)` | The last index, used in `X[end]` |
164+
| `firstindex(X)` | The first index |
165+
| `lastindex(X)` | The last index, used in `X[end]` |
166166

167167
For the `Squares` iterable above, we can easily compute the `i`th element of the sequence by squaring
168168
it. We can expose this as an indexing expression `S[i]`. To opt into this behavior, `Squares`
@@ -178,9 +178,8 @@ julia> Squares(100)[23]
178178
529
179179
```
180180

181-
Additionally, to support the syntax `S[begin]` and `S[end]`, we must define [`firstindex`](@ref) and
182-
[`lastindex`](@ref) to specify the first and last valid
183-
index, respectively:
181+
Additionally, to support the syntax `S[end]`, we must define [`lastindex`](@ref) to specify the last
182+
valid index. It is recommended to also define [`firstindex`](@ref) to specify the first valid index:
184183

185184
```jldoctest squaretype
186185
julia> Base.firstindex(S::Squares) = 1

doc/src/manual/strings.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ julia> """Contains "quote" characters"""
168168
If you want to extract a character from a string, you index into it:
169169

170170
```jldoctest helloworldstring
171-
julia> str[begin]
172-
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
173-
174171
julia> str[1]
175172
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
176173
@@ -183,8 +180,8 @@ julia> str[end]
183180

184181
Many Julia objects, including strings, can be indexed with integers. The index of the first
185182
element is returned by [`firstindex(str)`](@ref), and the index of the last element
186-
with [`lastindex(str)`](@ref). The keywords `begin` and `end` can be used inside an indexing
187-
operation as shorthand for the first and last index along the given dimension.
183+
with [`lastindex(str)`](@ref). The keyword`end` can be used inside an indexing
184+
operation as shorthand for the last index along the given dimension.
188185
Most indexing in Julia is 1-based: the first element of many integer-indexed objects is found at
189186
index 1. (As we will see below, this does not necessarily mean that the last element is found
190187
at index `n`, where `n` is the length of the string.)
@@ -203,7 +200,7 @@ julia> str[end÷2]
203200
Using an index less than 1 or greater than `end` raises an error:
204201

205202
```jldoctest helloworldstring
206-
julia> str[begin-1]
203+
julia> str[0]
207204
ERROR: BoundsError: attempt to access "Hello, world.\n"
208205
at index [0]
209206
[...]

src/julia-syntax.scm

+12-24
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@
8686
(define (expand-compare-chain e)
8787
(car (expand-vector-compare e)))
8888

89-
;; return the appropriate computation for a `begin` or `end` symbol for indexing
89+
;; return the appropriate computation for an `end` symbol for indexing
9090
;; the array `a` in the `n`th index.
9191
;; `tuples` are a list of the splatted arguments that precede index `n`
9292
;; `last` = is this last index?
93-
;; returns a call to, e.g., lastindex(a) or last(axes(a,n))
93+
;; returns a call to lastindex(a) or last(axes(a,n))
9494
(define (end-val a n tuples last)
9595
(if (null? tuples)
9696
(if (and last (= n 1))
@@ -101,31 +101,20 @@
101101
tuples))))
102102
`(call (top last) (call (top axes) ,a ,dimno)))))
103103

104-
(define (begin-val a n tuples last)
105-
(if (null? tuples)
106-
(if (and last (= n 1))
107-
`(call (top firstindex) ,a)
108-
`(call (top first) (call (top axes) ,a ,n)))
109-
(let ((dimno `(call (top +) ,(- n (length tuples))
110-
,.(map (lambda (t) `(call (top length) ,t))
111-
tuples))))
112-
`(call (top first) (call (top axes) ,a ,dimno)))))
113-
114-
;; replace `begin` and `end` for the closest ref expression, so doesn't go inside nested refs
115-
(define (replace-beginend ex a n tuples last)
104+
;; replace `end` for the closest ref expression, so doesn't go inside nested refs
105+
(define (replace-end ex a n tuples last)
116106
(cond ((eq? ex 'end) (end-val a n tuples last))
117-
((eq? ex 'begin) (begin-val a n tuples last))
118107
((or (atom? ex) (quoted? ex)) ex)
119108
((eq? (car ex) 'ref)
120109
;; inside ref only replace within the first argument
121-
(list* 'ref (replace-beginend (cadr ex) a n tuples last)
110+
(list* 'ref (replace-end (cadr ex) a n tuples last)
122111
(cddr ex)))
123112
(else
124113
(cons (car ex)
125-
(map (lambda (x) (replace-beginend x a n tuples last))
114+
(map (lambda (x) (replace-end x a n tuples last))
126115
(cdr ex))))))
127116

128-
;; go through indices and replace the `begin` or `end` symbol
117+
;; go through indices and replace the `end` symbol
129118
;; a = array being indexed, i = list of indices
130119
;; returns (values index-list stmts) where stmts are statements that need
131120
;; to execute first.
@@ -144,17 +133,17 @@
144133
(loop (cdr lst) (+ n 1)
145134
stmts
146135
(cons (cadr idx) tuples)
147-
(cons `(... ,(replace-beginend (cadr idx) a n tuples last))
136+
(cons `(... ,(replace-end (cadr idx) a n tuples last))
148137
ret))
149138
(let ((g (make-ssavalue)))
150139
(loop (cdr lst) (+ n 1)
151-
(cons `(= ,g ,(replace-beginend (cadr idx) a n tuples last))
140+
(cons `(= ,g ,(replace-end (cadr idx) a n tuples last))
152141
stmts)
153142
(cons g tuples)
154143
(cons `(... ,g) ret))))
155144
(loop (cdr lst) (+ n 1)
156145
stmts tuples
157-
(cons (replace-beginend idx a n tuples last) ret)))))))
146+
(cons (replace-end idx a n tuples last) ret)))))))
158147

159148
;; GF method does not need to keep decl expressions on lambda args
160149
;; except for rest arg
@@ -1511,8 +1500,7 @@
15111500
(idxs (cddr e)))
15121501
(let* ((reuse (and (pair? a)
15131502
(contains (lambda (x)
1514-
(or (eq? x 'begin)
1515-
(eq? x 'end)
1503+
(or (eq? x 'end)
15161504
(eq? x ':)
15171505
(and (pair? x)
15181506
(eq? (car x) ':))))
@@ -1527,7 +1515,7 @@
15271515

15281516
(define (expand-update-operator op op= lhs rhs . declT)
15291517
(cond ((and (pair? lhs) (eq? (car lhs) 'ref))
1530-
;; expand indexing inside op= first, to remove "begin", "end", and ":"
1518+
;; expand indexing inside op= first, to remove "end" and ":"
15311519
(let* ((ex (partially-expand-ref lhs))
15321520
(stmts (butlast (cdr ex)))
15331521
(refex (last (cdr ex)))

0 commit comments

Comments
 (0)