Skip to content

Instantly share code, notes, and snippets.

@kungfooman
Created June 19, 2017 21:03
Show Gist options
  • Save kungfooman/f91cb0eb7e3d2447c1e66696bd6a9068 to your computer and use it in GitHub Desktop.
Save kungfooman/f91cb0eb7e3d2447c1e66696bd6a9068 to your computer and use it in GitHub Desktop.
C's offsetof in julia
type ABC a::Int64; b::Int64; c::Int64 end
function offsetof(type_, member::Symbol)
for (i, item) in enumerate(fieldnames(type_))
if item == member
return fieldoffset(type_, i)
end
#print(typeof(i))
end
# what to do when symbol not in type_?
throw("$type_ has no member named $member")
end
#@code_native offsetof(ABC, :c)
offsetof(ABC, :a) # 0
offsetof(ABC, :b) # 8
offsetof(ABC, :c) # 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment