From 8f98b4161448d4a619a403ad13577e8fff1e19fa Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Mon, 22 Jan 2018 17:15:12 -0500 Subject: [PATCH] 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. --- NEWS.md | 5 +---- doc/src/manual/functions.md | 4 ++-- doc/src/manual/interfaces.md | 5 ++--- doc/src/manual/strings.md | 9 +++------ src/julia-syntax.scm | 36 ++++++++++++------------------------ 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/NEWS.md b/NEWS.md index c612af01aa435..fc320cc244824 100644 --- a/NEWS.md +++ b/NEWS.md @@ -44,9 +44,6 @@ New language features * Values for `Enum`s can now be specified inside of a `begin` block when using the `@enum` macro ([#25424]). - * `a[begin]` can now be used to address the first element of an integer-indexed collection `a`. - The index is computed by `beginindex(a)` ([#23554]). - Language changes ---------------- @@ -1246,4 +1243,4 @@ Command-line option changes [#25545]: https://github.com/JuliaLang/julia/issues/25545 [#25616]: https://github.com/JuliaLang/julia/issues/25616 [#25634]: https://github.com/JuliaLang/julia/issues/25634 -[#25654]: https://github.com/JuliaLang/julia/issues/25654 \ No newline at end of file +[#25654]: https://github.com/JuliaLang/julia/issues/25654 diff --git a/doc/src/manual/functions.md b/doc/src/manual/functions.md index e481c1668aefc..87c08656d8ec4 100644 --- a/doc/src/manual/functions.md +++ b/doc/src/manual/functions.md @@ -692,8 +692,8 @@ the results (see [Pre-allocating outputs](@ref)). A convenient syntax for this i is equivalent to `broadcast!(identity, X, ...)` except that, as above, the `broadcast!` loop is fused with any nested "dot" calls. For example, `X .= sin.(Y)` is equivalent to `broadcast!(sin, X, Y)`, overwriting `X` with `sin.(Y)` in-place. If the left-hand side is an array-indexing expression, -e.g. `X[begin+1:end] .= sin.(Y)`, then it translates to `broadcast!` on a `view`, e.g. -`broadcast!(sin, view(X, beginindex(X)+1:endindex(X)), Y)`, +e.g. `X[2:end] .= sin.(Y)`, then it translates to `broadcast!` on a `view`, e.g. +`broadcast!(sin, view(X, 2:endindex(X)), Y)`, so that the left-hand side is updated in-place. Since adding dots to many operations and function calls in an expression diff --git a/doc/src/manual/interfaces.md b/doc/src/manual/interfaces.md index f3d27bd1fdaad..0c3e719ea1460 100644 --- a/doc/src/manual/interfaces.md +++ b/doc/src/manual/interfaces.md @@ -178,9 +178,8 @@ julia> Squares(100)[23] 529 ``` -Additionally, to support the syntax `S[begin]` and `S[end]`, we must define [`beginindex`](@ref) and -[`endindex`](@ref) to specify the first and last valid -index, respectively: +Additionally, to support the syntax `S[end]`, we must define [`endindex`](@ref) to specify the last +valid index. It is recommended to also define [`beginindex`](@ref) to specify the first valid index: ```jldoctest squaretype julia> Base.beginindex(S::Squares) = 1 diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index c527e45bf7340..05ec45da4aeb8 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -168,9 +168,6 @@ julia> """Contains "quote" characters""" If you want to extract a character from a string, you index into it: ```jldoctest helloworldstring -julia> str[begin] -'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase) - julia> str[1] 'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase) @@ -183,8 +180,8 @@ julia> str[end] Many Julia objects, including strings, can be indexed with integers. The index of the first element is returned by [`beginindex(str)`](@ref), and the index of the last element -with [`endindex(str)`](@ref). The keywords `begin` and `end` can be used inside an indexing -operation as shorthand for the first and last index along the given dimension. +with [`endindex(str)`](@ref). The keyword`end` can be used inside an indexing +operation as shorthand for the last index along the given dimension. Most indexing in Julia is 1-based: the first element of many integer-indexed objects is found at index 1. (As we will see below, this does not necessarily mean that the last element is found at index `n`, where `n` is the length of the string.) @@ -203,7 +200,7 @@ julia> str[end÷2] Using an index less than 1 or greater than `end` raises an error: ```jldoctest helloworldstring -julia> str[begin-1] +julia> str[0] ERROR: BoundsError: attempt to access "Hello, world.\n" at index [0] [...] diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 7583382fdcdff..9f6572ac2fdb0 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -86,11 +86,11 @@ (define (expand-compare-chain e) (car (expand-vector-compare e))) -;; return the appropriate computation for a `begin` or `end` symbol for indexing +;; return the appropriate computation for an `end` symbol for indexing ;; the array `a` in the `n`th index. ;; `tuples` are a list of the splatted arguments that precede index `n` ;; `last` = is this last index? -;; returns a call to, e.g., endindex(a) or last(axes(a,n)) +;; returns a call to endindex(a) or last(axes(a,n)) (define (end-val a n tuples last) (if (null? tuples) (if (and last (= n 1)) @@ -101,31 +101,20 @@ tuples)))) `(call (top last) (call (top axes) ,a ,dimno))))) -(define (begin-val a n tuples last) - (if (null? tuples) - (if (and last (= n 1)) - `(call (top beginindex) ,a) - `(call (top first) (call (top axes) ,a ,n))) - (let ((dimno `(call (top +) ,(- n (length tuples)) - ,.(map (lambda (t) `(call (top length) ,t)) - tuples)))) - `(call (top first) (call (top axes) ,a ,dimno))))) - -;; replace `begin` and `end` for the closest ref expression, so doesn't go inside nested refs -(define (replace-beginend ex a n tuples last) +;; replace `end` for the closest ref expression, so doesn't go inside nested refs +(define (replace-end ex a n tuples last) (cond ((eq? ex 'end) (end-val a n tuples last)) - ((eq? ex 'begin) (begin-val a n tuples last)) ((or (atom? ex) (quoted? ex)) ex) ((eq? (car ex) 'ref) ;; inside ref only replace within the first argument - (list* 'ref (replace-beginend (cadr ex) a n tuples last) + (list* 'ref (replace-end (cadr ex) a n tuples last) (cddr ex))) (else (cons (car ex) - (map (lambda (x) (replace-beginend x a n tuples last)) + (map (lambda (x) (replace-end x a n tuples last)) (cdr ex)))))) -;; go through indices and replace the `begin` or `end` symbol +;; go through indices and replace the `end` symbol ;; a = array being indexed, i = list of indices ;; returns (values index-list stmts) where stmts are statements that need ;; to execute first. @@ -144,17 +133,17 @@ (loop (cdr lst) (+ n 1) stmts (cons (cadr idx) tuples) - (cons `(... ,(replace-beginend (cadr idx) a n tuples last)) + (cons `(... ,(replace-end (cadr idx) a n tuples last)) ret)) (let ((g (make-ssavalue))) (loop (cdr lst) (+ n 1) - (cons `(= ,g ,(replace-beginend (cadr idx) a n tuples last)) + (cons `(= ,g ,(replace-end (cadr idx) a n tuples last)) stmts) (cons g tuples) (cons `(... ,g) ret)))) (loop (cdr lst) (+ n 1) stmts tuples - (cons (replace-beginend idx a n tuples last) ret))))))) + (cons (replace-end idx a n tuples last) ret))))))) ;; GF method does not need to keep decl expressions on lambda args ;; except for rest arg @@ -1511,8 +1500,7 @@ (idxs (cddr e))) (let* ((reuse (and (pair? a) (contains (lambda (x) - (or (eq? x 'begin) - (eq? x 'end) + (or (eq? x 'end) (eq? x ':) (and (pair? x) (eq? (car x) ':)))) @@ -1527,7 +1515,7 @@ (define (expand-update-operator op op= lhs rhs . declT) (cond ((and (pair? lhs) (eq? (car lhs) 'ref)) - ;; expand indexing inside op= first, to remove "begin", "end", and ":" + ;; expand indexing inside op= first, to remove "end" and ":" (let* ((ex (partially-expand-ref lhs)) (stmts (butlast (cdr ex))) (refex (last (cdr ex)))