Created
July 6, 2014 13:58
-
-
Save SimonDanisch/1bf13d8cea9e7f358997 to your computer and use it in GitHub Desktop.
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
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