# Get type of field in a type

**URL:** <https://discourse.julialang.org/t/get-type-of-field-in-a-type/8795>\
**Category:** General Usage\
**Created:** [February 3, 2018, 3:19pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-a-type/8795 "2018-02-03T15:19:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [February 3, 2018, 3:19pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-a-type/8795/1 "2018-02-03T15:19:26Z")

</div>

Hi,

Say I have types:

```julia
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:

```julia
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}`?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 4, 2018, 12:15am UTC](https://discourse.julialang.org/t/get-type-of-field-in-a-type/8795/3 "2018-02-04T00:15:25Z")

</div>

```julia
julia> struct T
       a::Int
       b::Float64
       end

julia> fieldtype(T, :a)
Int64

julia> fieldtype(T, :b)
Float64

```

---

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [February 5, 2018, 10:55pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-a-type/8795/4 "2018-02-05T22:55:23Z")

</div>

Thanks @cstjean!
