# Which getindex expression threw BoundsError

**URL:** <https://discourse.julialang.org/t/which-getindex-expression-threw-boundserror/109329>\
**Category:** General Usage\
**Tags:** error-message\
**Created:** [January 27, 2024, 7:37am UTC](https://discourse.julialang.org/t/which-getindex-expression-threw-boundserror/109329 "2024-01-27T07:37:18Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 28, 2024, 6:27pm UTC](https://discourse.julialang.org/t/which-getindex-expression-threw-boundserror/109329/5 "2024-01-28T18:27:46Z")

</div>

> [@mnemnion](#):
>
> The workaround while debugging is to put each part of the expression on its own line, something like this:

Yes, but

> [@mnemnion](#):
>
> ```julia
> let n = 2, m = 3
> xs = randn(n)
> ys = randn(m)
> (xs[m] + 
> ys[n])
> end
> end
> 
> ```

This isn’t enough. Assigning `xs[m]` and `ys[n]` to variables is. The problem is that, after some compilation stage, Julia doesn’t know that the expression has more than one line. I guess that lowering is to blame here, according to Sukera?

I wonder how relevant this message still is (since then we got JuliaSyntax.jl and LLVM could have gotten some relevant improvements, too?):

> [@Make Julia’s Error Codes Even Better Than Elm’s](https://discourse.julialang.org/t/make-julia-s-error-codes-even-better-than-elm-s/87409/14):
>
> At a higher level, since it may not be clear to people, Julia is in a tricky spot when it comes to error messages. Static languages like Elm have a relatively slow compilation phase which doesn’t affect runtime. Note that the linked page is about compiler errors. The example given here is a runtime error—out of bounds index. (Genuine question: how are Elm’s runtime errors? Does it tell you what invalid index you used?) When an error occurs during compilation, if compilation is a separate step yo…

Somewhat related issues on Github:

> <https://github.com/JuliaLang/julia/issues/33735>
>
> I have some new or newish programmers and am trying to pay attention to their ro…adblocks in learning Julia, so I may occasionally file issues which don't look like my usual fare.
> 
> Suppose you try to define a variable using a name that starts with a number:
> \`\`\`julia
> julia\> 2x = 22
> ERROR: syntax: "2" is not a valid function argument name
> \`\`\`
> A new programmer might wonder, why does this talk about a "function argument name"? I haven't called any functions (or so I thought).
> 
> Python is not great at this, but the differences are interesting:
> \`\`\`python
> \>\>\> 2x = 22
> File "\<stdin\>", line 1
> 2x = 22
> ^
> SyntaxError: invalid syntax
> \>\>\>
> \`\`\`
> "invalid syntax" isn't very helpful, but python partly makes up for this with the caret syntax showing the specific place in the line that causes the trouble. (In this case, disambiguating which 2 is the problematic one.)
> 
> C (gcc) is much better, though still lacks the key hint that variable names should not start with a number:
> \`\`\`c
> \#include \<stdio.h\>
> 
> int main(void) {
> int 2x = 22;
> return 0;
> }
> \`\`\`
> 
> \`\`\`sh
> $ gcc c.c
> c.c: In function ‘main’:
> c.c:4:9: error: invalid suffix "x" on integer constant
> int 2x = 22;
> ^~
> c.c:4:9: error: expected identifier or ‘(’ before numeric constant
> \`\`\`
> 
> Matlab isn't perfect either, but it goes out of its way to be helpful:
> \`\`\`matlab
> \>\> 2x = 22
> 2x = 22
> ↑
> Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or
> other syntax error. To construct matrices, use brackets instead of parentheses.
>  
> Did you mean:
> \>\> x = 22
> \`\`\`
> (To clarify, that \`x = 22\` is queued up on the REPL.)
> 
> To me, the biggest takeaway is that most other languages have decided that a caret mark is a crucial component of the parser's error-reporting.

> <https://github.com/JuliaLang/julia/issues/38531>
>
> Reduction from a larger struct in which I was specifying the type of the \`nb\` fi…eld incorrectly:
> \`\`\`
> melis@juggle 23:28:~$ cat t.jl
> struct S
> field1::Bool
> field2::String
> # Error is here
> nb::Matrix{UInt8, UInt8}
> end
> melis@juggle 23:28:~$ julia t.jl
> ERROR: LoadError: too many parameters for type
> Stacktrace:
> \[1\] top-level scope at /home/melis/t.jl:1
> in expression starting at /home/melis/t.jl:1
> \`\`\`
> 
> The error line reported is the start of the struct definition, which is only of limited value. As I'm a julia beginner I don't immediately spot these errors within the struct and need to check for the offending line by commenting several fields and rerunning. Would it be possible to report the actual line involved? This is especially useful when structs start to contain many fields.
> 
> \`\`\`
> julia\> versioninfo()
> Julia Version 1.5.2
> Commit 539f3ce943\* (2020-09-23 23:17 UTC)
> Platform Info:
> OS: Linux (x86\_64-pc-linux-gnu)
> CPU: Intel(R) Core(TM) i5-4460 CPU @ 3.20GHz
> WORD\_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-10.0.1 (ORCJIT, haswell)
> \`\`\`

New issue:

> <https://github.com/JuliaLang/julia/issues/53090>
>
> \`a.jl\`:
> 
> \`\`\`julia
> 1 +
> 2 +
> 3 +
> error() +
> 5
> \`\`\`
> 
> \`b.jl\`:
> 
> \`\`\`julia
> 1 …+ 2 + 3 + 4 + error() + 5
> \`\`\`
> 
> REPL session:
> 
> \`\`\`julia-repl
> julia\> include("a.jl") # It'd be nice to get a more accurate line number here, i.e., "4" instead of "1"
> ERROR: LoadError: 
> Stacktrace:
> \[1\] error()
> @ Base ./error.jl:44
> \[2\] top-level scope
> @ /tmp/asdoi98uz/a.jl:1
> \[3\] include(fname::String)
> @ Main ./sysimg.jl:38
> \[4\] top-level scope
> @ REPL\[1\]:1
> in expression starting at /tmp/asdoi98uz/a.jl:1
> 
> julia\> include("b.jl") # It'd be nice to get a column number range here for the offending call, \`error()\`
> ERROR: LoadError: 
> Stacktrace:
> \[1\] error()
> @ Base ./error.jl:44
> \[2\] top-level scope
> @ /tmp/asdoi98uz/b.jl:1
> \[3\] include(fname::String)
> @ Main ./sysimg.jl:38
> \[4\] top-level scope
> @ REPL\[2\]:1
> in expression starting at /tmp/asdoi98uz/b.jl:1
> 
> julia\> versioninfo()
> Julia Version 1.11.0-DEV.1389
> Commit ecc14ca3888 (2024-01-27 15:45 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: Linux (x86\_64-linux-gnu)
> CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
> WORD\_SIZE: 64
> LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
> Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
> \`\`\`
> 
> 
> Related issues: #33735, #38531

---

_[View the full topic](https://discourse.julialang.org/t/which-getindex-expression-threw-boundserror/109329)._
