Do you actually need a type declaration like this, or could you write the equivalent of ```Julia immutable Foo2{M, N, T} ab::NTuple{M+N, T} end ``` and the introduce functions `geta` and getb` for `Foo`? This would be a completely type-safe solution.
On the other hand, if you are sure that the memory layout is as you expect, you can always go the C way: Get a pointer to `Foo`, unsafe-cast that pointer to `Foo2`, and access the elements via `Foo2`. -erik On Sun, Mar 13, 2016 at 7:57 AM, Kristoffer Carlsson <kcarlsso...@gmail.com> wrote: > Hello everyone. > > Assume I have an immutable like: > > immutable Foo{M, N, T} > a::NTuple{M, T} > b::NTuple{N, T} > end > > and I want to make an efficient getindex into the tuple. > > Something that would probably be inefficient due to branching would be: > > function Base.getindex{M, N}(f::Foo({M,N}), i) > if i <= M > return f.a[i] > else > return f.b[i - N] > end > end > > Is there anyway I can just grab the value at the address at an offset i - 1 > further forward of the address of f.a[1]. In my head this would be more > efficient since it would be a very simple offset calculation. You can assume > that T is a nice bits type like a Float32. > > Thanks! > > // Kristoffer > > > > -- Erik Schnetter <schnet...@gmail.com> http://www.perimeterinstitute.ca/personal/eschnetter/