Created
June 19, 2017 21:03
-
-
Save kungfooman/f91cb0eb7e3d2447c1e66696bd6a9068 to your computer and use it in GitHub Desktop.
C's offsetof in julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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