# Get nominal type of field of composite type?

**URL:** <https://discourse.julialang.org/t/get-nominal-type-of-field-of-composite-type/4736>\
**Category:** New to Julia\
**Created:** [July 8, 2017, 2:45am UTC](https://discourse.julialang.org/t/get-nominal-type-of-field-of-composite-type/4736 "2017-07-08T02:45:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 8, 2017, 2:45am UTC](https://discourse.julialang.org/t/get-nominal-type-of-field-of-composite-type/4736/1 "2017-07-08T02:45:40Z")

</div>

How would one determine the _nominal_ type of a field in a composite type?

For example, suppose I have something like this:

```julia
mutable struct MyType
  sub::Number
end

function foo(x::Any)

  # I can get the type of whatever is stored in "sub" like so:
  typeof(x.sub) # For our example, this returns Float64, not Number

  # But suppose I want to know what 'sub' is allowed to be, 
  # like is it always a Float64? Or a Number? Or an Any?

end

x = MyType(2.7);
foo(x) # I'd like this to return Number, not Float64.

```

More generally, how do I explore the type MyType?

(While we’re here, is there some way to declare that the input “x” must have a field called “sub” without specifying any particular abstract type?)

This if for a some flexible conversion utilities where performance is totally unimportant.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [July 8, 2017, 2:48am UTC](https://discourse.julialang.org/t/get-nominal-type-of-field-of-composite-type/4736/2 "2017-07-08T02:48:43Z")

</div>

> More generally, how do I explore the type MyType?

dump(MyType)

> How would one determine the nominal type of a field in a composite type?

julia\> Any[fieldtype(MyType, i) for i in 1:nfields(MyType)]  
1-element Array{Any,1}:  
Number

> While we’re here, is there some way to declare that the input “x” must have a field called “sub” without specifying any particular abstract type?

Yes: write tests. 🙂

---

<div class="post-metadata">

**Author:** ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)\
**Post date:** [July 8, 2017, 1:48pm UTC](https://discourse.julialang.org/t/get-nominal-type-of-field-of-composite-type/4736/3 "2017-07-08T13:48:49Z")

</div>

Thanks, @jameson! `fieldtype` (“Determine the declared type of a field (specified by name or index) in a composite DataType…”) is exactly what I was looking for.

(And for the bonus question: I guess I can use an assert to write a message like, “Hey, input ‘x’ is supposed to have a field called ‘sub’,” but I didn’t know if there was some better way to handle this.)
