Created
July 6, 2014 13:59
-
-
Save SimonDanisch/f1d88529dbd46c666eb5 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) | |
# type checks | |
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) # type must have at least one field | |
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