And if you want to reduce memory allocations and your fields are all of the same type:
immutable Foo{T}
a::T
b::T
c::T
end
# this always works also for mutables / varying fieldtypes:
typevec(a) = [a.(x) for x in names(a)]
# create initial view
typevec!(foo::Foo) = pointer_to_array(
convert(Ptr{typeof(foo.a)}, pointer_from_objref(foo))+sizeof(typeof(foo.a)), 3)
# point to a different obj, reusing the view array
function typevec!(a, view)
p = convert(Ptr{eltype(view)}, pointer_from_objref(view))
unsafe_store!(p, pointer_from_objref(a)+sizeof(eltype(view)), 2)
view
end
foo = Foo(1,2,3)
foo2 = Foo(10,20,20)
@show @time typevec(foo)
@show @time view = typevec!(foo)
@show @time typevec!(foo2,view)
Just be careful that foo does not go out of scope while you use the result of "typevec!"