# A(i,j) instead of A\[i,j\] leads to weird behavior

**URL:** <https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196>\
**Category:** General Usage\
**Created:** [January 28, 2019, 9:53pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196 "2019-01-28T21:53:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![murphyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murphyk/32/6891_2.png) [@murphyk](https://discourse.julialang.org/u/murphyk)\
**Post date:** [January 28, 2019, 9:53pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/1 "2019-01-28T21:53:38Z")

</div>

Consider the code below.

```julia
function dummy()
    A = zeros(2,2)
    for i=1:2
        for j=1:2
            A(i,j) = rand() # should be A[i,j]
        end
    end
    return A
end

```

It returns

```julia
julia> A=dummy()
A (generic function with 1 method)

```

which is very weird, since in the REPL writing A(i,j) instead of A[i,j] returns an error.  
Is this a bug?

By contrast, if we use A[i,j] = rand() we get

```julia
julia> A=dummy()
2×2 Array{Float64,2}:
 0.531469 0.218812
 0.48418 0.581266

```

as expected.

Note that odd behavior also happens outside of functions, although it seems to have to be inside of a for loop. For example

```julia
A = zeros(2,2)
for i=1:2
    for j=1:2
        A(i,j) = rand() # should be A[i,j] = rand()
    end
end

```

returns zeros(2,2).  
Whereas if I write

```julia
A = zeros(2,2); A(1,1) = rand(); A(1,2) = rand(); A(2,1)=rand(); A(2,2)=rand()

```

I get a syntax error, as expected:

```julia
ERROR: syntax: "1" is not a valid function argument name

```

What is going on?

---

<div class="post-metadata">

**Author:** ![murphyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murphyk/32/6891_2.png) [@murphyk](https://discourse.julialang.org/u/murphyk)\
**Post date:** [January 28, 2019, 9:57pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/2 "2019-01-28T21:57:27Z")

</div>

Never mind! I figured it out. It’s the inline function definition syntax tripping me up.  
A(i,j) = rand() is defining a function that maps its 2 args to a random number.

This is a nasty “gotcha” for ex Matlab prorgammers to be aware of!

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 28, 2019, 10:02pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/3 "2019-01-28T22:02:37Z")

</div>

You’re not the only one:

> [@No error when using () for indexing inside a loop](https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970):
>
> 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 …

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [January 30, 2019, 1:28pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/4 "2019-01-30T13:28:23Z")

</div>

Using the same syntax for indexing as function calls is a bug in the Matlab syntax. (It leads to strange behavior in edge cases.) At least in Julia, when things go wrong it’s because of a programmer error.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [January 30, 2019, 1:46pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/5 "2019-01-30T13:46:31Z")

</div>

This may get me expelled from class, but occasionally I define this…

```julia
(A::Array)(ijk...) = A[ijk...]

```

Obviously this is a terrible idea! But it shouldn’t break valid Julia code, and would turn the above loop into an error. Which which can be useful on the first pass of trying to hack some Matlab script you found in the gutter into running Julia.

Edit – I lied, it does not produce an error here. Because the for loop makes a new scope. Moving on…

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 30, 2019, 3:31pm UTC](https://discourse.julialang.org/t/a-i-j-instead-of-a-i-j-leads-to-weird-behavior/20196/6 "2019-01-30T15:31:13Z")

</div>

Yeah, we have a nice error message when you try to access `A(i, j)`, but trying to assign into it like this is a much harder case to catch — namely because it actually means something different!
