No error when using () for indexing inside a loop

I accidentally came across this observation, when trying to create a symmetric matrix.

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

julia> z = randn(5,5)
5×5 Array{Float64,2}:
  1.35901    -0.507248   1.60027     1.53572   -1.51329 
 -0.0657509  -0.91781    0.0559653  -0.133623   1.06091 
  0.434098    1.0356    -0.545556    0.3141     0.595068
  1.17286     0.88539    0.175035    0.258012  -1.66506 
  1.32398    -1.16009    0.238903   -0.468032   0.70049 

julia> for i = 1:size(z,1)
         for j = 1:i
           z(i,j) = z(j,i)    # Shouldn't it have raised an error here?
         end
       end

julia> z
5×5 Array{Float64,2}:
  1.35901    -0.507248   1.60027     1.53572   -1.51329 
 -0.0657509  -0.91781    0.0559653  -0.133623   1.06091 
  0.434098    1.0356    -0.545556    0.3141     0.595068
  1.17286     0.88539    0.175035    0.258012  -1.66506 
  1.32398    -1.16009    0.238903   -0.468032   0.70049 

But if I do without loop, it throws error

julia> i = 2; j = 3;

julia> z(i,j)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] top-level scope at none:0

Is this expected behavior?

No, this is creating a new local function called z.

3 Likes

Thanks.
That explains it. :slight_smile: