Yes -- this would be awesome! This functionality be most useful in many physics simulation codes, where various user-defined types are implicitly elements of a vector space, and being able to treat them as arrays would simplify coding a lot.
Several possible implementations come to my mind: (1) Explicitly define getindex and setindex!, and a few other array operations, so that the type behaves like an array from Julia's point of view (2) Define the type as array or as tuple, and explicitly define functions ".exx" etc. that offer a syntax identical or similar to accessing field elements (3) Allow "reinterpret" in this case (immutable types where all fields have the same type) (4) Do one of the above, or something else, and use macros in a clever way to simplify the coding overhead -erik > On Mar 17, 2015, at 11:08 , Kristoffer Carlsson <[email protected]> wrote: > > Say I have an immutable with only one type. > > immutable Strain > exx::Float64 > eyy::Float64 > ezz::Float64 > gyz::Float64 > gxz::Float64 > gxy::Float64 > end > > Is there anyway I can reinterpret this as a Vector{Float64} in a fast way > without copying data. The memory of the Strain type should be exactly like > the vector. > > The reason I want to do this is because I want to i.e. be able to multiply a > strain and a general matrix but I also want to be able to write my own > functions for Strain. > > Naively, I tried this: > > julia> reinterpret(Vector{Float64}, strain) > ERROR: reinterpret: expected bits type as first argument > in reinterpret at base.jl:65 > > I saw this comment in one of the issues here > https://github.com/JuliaLang/julia/issues/5857: > > "ImmutableArrays has the advantage that Array(Vector4{Int}, 10) it can be > reinterpreted as a 4x10 matrix with reinterpret and reshape without memory > movement (e.g. when calling a c function on the data)." > > > Best regards, > Kristoffer Carlsson -- Erik Schnetter <[email protected]> http://www.perimeterinstitute.ca/personal/eschnetter/ My email is as private as my paper mail. I therefore support encrypting and signing email messages. Get my PGP key from https://sks-keyservers.net.
signature.asc
Description: Message signed with OpenPGP using GPGMail
