Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SimonDanisch/1bf13d8cea9e7f358997 to your computer and use it in GitHub Desktop.
Save SimonDanisch/1bf13d8cea9e7f358997 to your computer and use it in GitHub Desktop.
function fieldtype(x::Any)
T = typeof(x)
if !isbits(T)
error(T, " is not a bitstype, so it doesn't have the right memory layout")
end
types = T.types
@assert !isempty(types)
ftype = types[1]
if any(t -> t!= ftype, types)
error("field types are not homogenious for: ", T)
end
# We can return the field type,
# this means we can make a more specific function for this type
eval(( :(fieldtype(x::$T) = $ftype) ))
return ftype
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment