Get type of field in a type


#1

Hi,

Say I have types:

abstract type abtype end

struct mytype <: abtype
    a::UInt8
end

struct mytype2 <: abtype
    a::UInt64
end

... and so on for several more concrete types of abtype

Say I want to get the type of a field a for any subtype of the abstract type. If I have an instance of the type I can do this:

getatype(x::abtype) = typeof(x.a)

BUT is there a way I might do this generally using the datatype(s) - without needing an instance of them, like some sort of getatype(x::Type{T}) where {T <: abtype}?


#3
julia> struct T
       a::Int
       b::Float64
       end

julia> fieldtype(T, :a)
Int64

julia> fieldtype(T, :b)
Float64

#4

Thanks @cstjean!