3 ms·
> If you pass it an array with an unusual index range, it will access out-of-bounds memory: the array access was annotated with @inbounds, which removed the bou
by RcouF1uZ4gsC 4y ago
> If you pass it an array with an unusual index range, it will access out-of-bounds memory: the array access was annotated with @inbounds, which removed the bounds check.
It think making indexes configurable is a huge mistake. Even if they are not ideal for the situation, having a single way to do indexes makes a huge source of confusion and potential bugs just go away. And this is orthogonal to whether you pick 0 or 1 as your starting point, as long as the whole language embraces that.
For example with C/C++/Rust, you know it is zero based indexing. Even if it is not perfectly ideal for your formulas, the mental math of translating to zero based is with not constantly having to worry about if a library is one based or zero based and what happens if you compose them.
- adgjlsfhk1 4y agoOffsetArrays can be really nice for things like convolutions. For example, it ends up being really natural to have a matrix that is indexed on [-2:2, -2:2] to implement a gausian blur. It definitely is a potential bug source though.
- kllrnohj 4y agoIndexes being configurable makes a ton of sense. It's why so many languages end up with a slice type (or array_view or span or whatever you want to call it). Why shouldn't the base array type just itself be the slice type?
- mattkrause 4y agoThere's a parallel idea, that you should avoid--insofar as is possible--numerical indexing. In other words, instead of iterating over `0:length(X) - 1` or `1:length(X)`, you use something like `for element in array` or indices = CartesianIndices(multidimensional_X) for index in indices X[index] = # whatever If you do that, you don't need to keep track of whether it's zero-based, one-based, or anything else. In fact, you may not even need to keep track of the number of dimensions, as in this example, https://julialang.org/blog/2016/02/iteration/ https://julialang.org/blog/2016/02/iteration/
- runevault 4y agoI'm only skimming this post and I'm not familiar with Julia so maybe I'm missing it, but does it have a way to get an item AND its index? There's I think Enumrable? in Rust where it gives you a tuple with both the item and its index in cases where you need both.
- lmiq 4y agofor (i,val) in pairs(array)
- leephillips 4y agojulia> pairs("François") |> collect 8-element Vector{Pair{Int64, Char}}: 1 => 'F' 2 => 'r' 3 => 'a' 4 => 'n' 5 => 'ç' 7 => 'o' 8 => 'i' 9 => 's' Notice the missing index 6, because ç takes two bytes. In contrast, enumerate() gets you the iteration number: julia> enumerate("François") |> collect 8-element Vector{Tuple{Int64, Char}}: (1, 'F') (2, 'r') (3, 'a') (4, 'n') (5, 'ç') (6, 'o') (7, 'i') (8, 's') This can trip you up.
- DNF2 4y agoWhat do you mean by 'mistake'? How are the Julia devs going to stop someone from defining arrays with configurable indices? Are you suggesting that the core language should somehow make this impossible? How?