You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add error hint for incorrect stacked indexing (#40934)
A common entry level mistake is to try to index a matrix with two sets
of brackets, e.g. `a = [1 2; 3 4]; a[1][2] = 5`
This will lead to an error that `setindex!()` on the element type of `a`
is missing.
This PR adds an error hint for the case where a MethodError is raised
when `setindex!` is called with a `Number` as the first argument.
I considered going broader than numbers, but it seems more likely that
this kind of mistake would happen when working with simple number arrays
vs. something more advanced.
Could also consider if it is possible to do the same for when
`getindex()` is called on a `Number`, which emits a BoundsError.
Co-authored-by: Michael Abbott <[email protected]>
@testcount(==("Maybe you forgot to use an operator such as *, ^, %, / etc. ?"), split(err_str, '\n')) ==1
746
748
end
747
749
750
+
let err_str
751
+
a = [12; 34];
752
+
err_str =@except_str (a[1][2] =5) MethodError
753
+
@testoccursin("\nAre you trying to index into an array? For multi-dimensional arrays, separate the indices with commas: ", err_str)
754
+
@testoccursin("a[1, 2]", err_str)
755
+
@testoccursin("rather than a[1][2]", err_str)
756
+
end
757
+
758
+
let err_str
759
+
d = Dict
760
+
err_str =@except_str (d[1] =5) MethodError
761
+
@testoccursin("\nYou attempted to index the type Dict, rather than an instance of the type. Make sure you create the type using its constructor: ", err_str)
762
+
@testoccursin("d = Dict([...])", err_str)
763
+
@testoccursin(" rather than d = Dict", err_str)
764
+
end
765
+
748
766
# Execute backtrace once before checking formatting, see #38858
0 commit comments