What is the current (and future) status of `begin` keyword in indexing?


#1

I’ve started moving my projects to Julia 1.0. I find that the begin keyword in array index has been removed. I get an error trying to use it

julia> x = [1,2,3,4]
4-element Array{Int64,1}:
 1
 2
 3
 4
x[begin:end]
┌ Warning: Deprecated syntax `"begin" inside indexing expression` at REPL[19]:1.
└ @ REPL[19]:1
ERROR: syntax: extra token "]" after end of expression

julia> x[begin]
┌ Warning: Deprecated syntax `"begin" inside indexing expression` at REPL[21]:1.
└ @ REPL[21]:1
ERROR: syntax: unexpected "]"

The release note of 0.7 says that

begin is disallowed inside indexing expressions, in order to enable the syntax a[begin] (for selecting the first element) in the future (#23354).

I’m a bit confused. Does this mean that in a future version, a[begin] will be allowed but a[begin:end-2] will be illegal?

Thanks!


#2

In Julia 0.6 it was valid to do

julia> a = rand(5)
5-element Array{Float64,1}:
 0.0143824 
 0.359066  
 0.568123  
 0.00120256
 0.547529  

julia> a[begin
             x = rand(1:2)
             y = rand(1:3)
             x + y
         end]
0.5475289624323161

This has been deprecated so that it will be possible in the future to allow syntax like a[begin], a[begin:end], a[begin:end-2] and so on to work with begin as an indexing counterpart to end, rather than starting a block inside the indexing.


#3

If you do want the previous behaviour for any reason, you can still use brackets to achieve it. The example becomes

a[(x = rand(1:2); y = rand(1:3); x + y)]

#4

Thank you! I didn’t know that a begin-block was allowed in the index in 0.6.x. Felt a bit hackish though and I probably wouldn’t feel comfortable to use it anyway. Reserving the begin keyword to mean the first index seems a great decision to me.


#5

Thanks for the tip! This looks neater than the begin ... end block to me. If I have more complicated indexing expressions I would probably use a list comprehension.