One way to make OffsetArrays safe would be to make them index normally with 
integer indices, but introduce a *new* Integer type that gives it the 
special offset behavior.  You could name it something short, like F (for 
Fortran or oFFset) to reduce typing:

`A[F(-10), F(0)]`

... and you could do even better by making it construct by multiplication 
with the typename:

`A[-10F, 0F]`

Or even better, you should be able to write a macro that automatically 
converts all indices to this special type:

`@offset A[-10,0]` # This also could disallow the pesky end keyword!

I was playing with RaggedArrays over the weekend, and there I had to solve 
a similar problem because there are two possible meanings for linear 
indexing.  So I just created a LinearIndex type that behaves 
differently: 
https://github.com/mbauman/RaggedArrays.jl/blob/d2b6aeb854855c7912c104cb5312c13e989f2cf4/src/core.jl#L229-L247

Matt

On Monday, August 17, 2015 at 4:36:27 PM UTC-4, lawrence dworsky wrote:
>
> Thanks for putting your time into this. Right now I'm still using 0.3.11, 
> waiting for 0.4 to be the standard release. Then I'll dig into this and see 
> if I get it to do what I want without undue aggravation. I had the same 
> indexing issue with MatLab. Sometimes I miss the brute straightforwardness 
> of Fortran.
>
> Larry
>
>
> On Mon, Aug 17, 2015 at 1:16 PM, Matt Bauman <[email protected] 
> <javascript:>> wrote:
>
>> On Monday, August 17, 2015 at 1:03:17 PM UTC-4, Sisyphuss wrote:
>>>
>>> I read the "interfaces 
>>> <http://docs.julialang.org/en/latest/manual/interfaces/>" chapter of 
>>> the documentation today. I learned that, if you define an iterable as a 
>>> subtype of AbstractArray, with only defining three methods (including 
>>> `size()`, excluding `start()`), you can iterate on it just like iterate on 
>>> an normal Array.
>>>
>>
>> Iteration should work just fine in 0.4 if OffsetArray defines its own 
>> `eachindex` method.
>>
>> Although more and more for loops are written generically using 
>> `eachindex`, there are still a lot of methods that use the old linear 
>> indexing standby:
>>
>> for i=1:length(A)
>>    @inbounds A[i] = …
>> end
>>
>> This is where things get really hairy for OffsetArrays.  That `@inbounds` 
>> propagates through to the inner array assignment, which will lead to silent 
>> data corruption and/or segfaults.  That's really why it shouldn't be an 
>> AbstractArray.
>>
>
>

Reply via email to