Skip to content

Commit ca99175

Browse files
committed
Remove special syntax for : in indexing []
Add news and a minor documentation update.
1 parent 965f84e commit ca99175

File tree

3 files changed

+23
-40
lines changed

3 files changed

+23
-40
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ Language changes
114114
macro. Instead, the string is first unindented and then `x_str` is invoked,
115115
as if the string had been single-quoted ([#10228]).
116116

117+
* Colons (`:`) within indexing expressions are no longer lowered to the range
118+
`1:end`. Instead, the `:` identifier is passed directly. Custom array types
119+
that implement `getindex` or `setindex!` methods must also extend those
120+
methods to support arguments of type `Colon` ([#10331]).
121+
117122
Command line option changes
118123
---------------------------
119124

@@ -1422,6 +1427,7 @@ Too numerous to mention.
14221427
[#10180]: https://github.com/JuliaLang/julia/issues/10180
14231428
[#10228]: https://github.com/JuliaLang/julia/issues/10228
14241429
[#10328]: https://github.com/JuliaLang/julia/issues/10328
1430+
[#10331]: https://github.com/JuliaLang/julia/issues/10331
14251431
[#10332]: https://github.com/JuliaLang/julia/issues/10332
14261432
[#10333]: https://github.com/JuliaLang/julia/issues/10333
14271433
[#10380]: https://github.com/JuliaLang/julia/issues/10380

doc/manual/arrays.rst

+9-7
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ The general syntax for indexing into an n-dimensional array A is::
222222
where each ``I_k`` may be:
223223

224224
1. A scalar integer
225-
2. A ``Range`` of the form ``:``, ``a:b``, or ``a:b:c``
226-
3. An arbitrary integer vector, including the empty vector ``[]``
227-
4. A boolean vector
225+
2. A ``Range`` of the form ``a:b``, or ``a:b:c``
226+
3. A ``:`` or ``Colon()`` to select entire dimensions
227+
4. An arbitrary integer vector, including the empty vector ``[]``
228+
5. A boolean vector
228229

229230
The result ``X`` generally has dimensions
230231
``(length(I_1), length(I_2), ..., length(I_n))``, with location
@@ -286,10 +287,11 @@ The general syntax for assigning values in an n-dimensional array A is::
286287

287288
where each ``I_k`` may be:
288289

289-
1. A scalar value
290-
2. A ``Range`` of the form ``:``, ``a:b``, or ``a:b:c``
291-
3. An arbitrary integer vector, including the empty vector ``[]``
292-
4. A boolean vector
290+
1. A scalar integer
291+
2. A ``Range`` of the form ``a:b``, or ``a:b:c``
292+
3. A ``:`` or ``Colon()`` to select entire dimensions
293+
4. An arbitrary integer vector, including the empty vector ``[]``
294+
5. A boolean vector
293295

294296
If ``X`` is an array, its size must be ``(length(I_1), length(I_2), ..., length(I_n))``,
295297
and the value in location ``i_1, i_2, ..., i_n`` of ``A`` is overwritten with

src/julia-syntax.scm

+8-33
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,17 @@
301301
(define (expand-compare-chain e)
302302
(car (expand-vector-compare e)))
303303

304-
;; last = is this last index?
304+
;; return the appropriate computation for an `end` symbol for indexing
305+
;; the array `a` in the `n`th index.
306+
;; `tuples` are a list of the splatted arguments that precede index `n`
307+
;; `last` = is this last index?
308+
;; returns a call to endof(a), trailingsize(a,n), or size(a,n)
305309
(define (end-val a n tuples last)
306310
(if (null? tuples)
307311
(if last
308312
(if (= n 1)
309313
`(call (top endof) ,a)
310314
`(call (top trailingsize) ,a ,n))
311-
#;`(call (top div)
312-
(call (top length) ,a)
313-
(call (top *)
314-
,@(map (lambda (d) `(call (top size) ,a ,(1+ d)))
315-
(iota (- n 1)))))
316315
`(call (top size) ,a ,n))
317316
(let ((dimno `(call (top +) ,(- n (length tuples))
318317
,.(map (lambda (t) `(call (top length) ,t))
@@ -321,8 +320,7 @@
321320
`(call (top trailingsize) ,a ,dimno)
322321
`(call (top size) ,a ,dimno)))))
323322

324-
; replace end inside ex with (call (top size) a n)
325-
; affects only the closest ref expression, so doesn't go inside nested refs
323+
; replace `end` for the closest ref expression, so doesn't go inside nested refs
326324
(define (replace-end ex a n tuples last)
327325
(cond ((eq? ex 'end) (end-val a n tuples last))
328326
((or (atom? ex) (quoted? ex)) ex)
@@ -335,29 +333,7 @@
335333
(map (lambda (x) (replace-end x a n tuples last))
336334
(cdr ex))))))
337335

338-
; translate index x from colons to ranges
339-
(define (expand-index-colon x)
340-
(cond ((eq? x ':) `(call colon 1 end))
341-
((and (pair? x)
342-
(eq? (car x) ':))
343-
(cond ((length= x 3)
344-
(if (eq? (caddr x) ':)
345-
;; (: a :) a:
346-
`(call colon ,(cadr x) end)
347-
;; (: a b)
348-
`(call colon ,(cadr x) ,(caddr x))))
349-
((length= x 4)
350-
(if (eq? (cadddr x) ':)
351-
;; (: a b :) a:b:
352-
`(call colon ,(cadr x) ,(caddr x) end)
353-
;; (: a b c)
354-
`(call colon ,@(cdr x))))
355-
(else x)))
356-
(else x)))
357-
358-
;; : inside indexing means 1:end
359-
;; expand end to size(a,n),
360-
;; or div(length(a), prod(size(a)[1:(n-1)])) for the last index
336+
;; go through indices and replace the `end` symbol
361337
;; a = array being indexed, i = list of indexes
362338
;; returns (values index-list stmts) where stmts are statements that need
363339
;; to execute first.
@@ -386,8 +362,7 @@
386362
(cons `(... ,g) ret))))
387363
(loop (cdr lst) (+ n 1)
388364
stmts tuples
389-
(cons (replace-end (expand-index-colon idx) a n tuples last)
390-
ret)))))))
365+
(cons (replace-end idx a n tuples last) ret)))))))
391366

392367
(define (make-decl n t) `(|::| ,n ,t))
393368

0 commit comments

Comments
 (0)