# Avoiding repeated type declaration

**URL:** <https://discourse.julialang.org/t/avoiding-repeated-type-declaration/100679>\
**Category:** General Usage\
**Created:** [June 22, 2023, 12:52am UTC](https://discourse.julialang.org/t/avoiding-repeated-type-declaration/100679 "2023-06-22T00:52:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Boylan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ross_boylan/32/9210_2.png) [@Ross\_Boylan](https://discourse.julialang.org/u/Ross_Boylan)\
**Post date:** [June 22, 2023, 12:52am UTC](https://discourse.julialang.org/t/avoiding-repeated-type-declaration/100679/1 "2023-06-22T00:52:50Z")

</div>

I find myself repeating a somewhat cumbersome type declaration, which seems like something to avoid. How? Here’s a condensed version of my current code, which uses `Vector{Union{Missing, IntervalBox{1, Float64}}}` twice.

```julia
"enumerate desired calculation for WorkArea"
# implicit value assignment starts at 0. But I want to use these as array indices
# and julia arrays are 1-based.
@enum Objective justZ=1 justW=2 WZ=3 just1=4

"""
Working data for a particular thread.
"""
mutable struct WorkArea{TEvaluator,TSegs}
    # ... many fields omitted
    "record details of the integration. Int(Objective) has result for that Objective"
    intervals::Vector{Union{Missing, IntervalBox{1, Float64}}}
end

"convenient constructor"
function WorkArea(dat::DataFrame, ev::TEvaluator) where {TEvaluator}
    zip::UInt = 0
    WorkArea(dat, dat.Y, ev, work(ev), WZ, zip, zip, zip, Vector{Union{Missing, IntervalBox{1, Float64}}}(missing, 4))
end

```

I think the type in the constructor is mandatory to get the correct type; I could omit it from the `struct` definition, with perhaps some loss of efficiency.

I looked for something that would give me the type of a component of a `struct`, something like `typeof(Workarea.intervals)`, but didn’t find it.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 22, 2023, 12:59am UTC](https://discourse.julialang.org/t/avoiding-repeated-type-declaration/100679/2 "2023-06-22T00:59:08Z")

</div>

You could just declare a type constant.

```julia
const IntervalBoxVector = Vector{Union{Missing, IntervalBox{1, Float64}}}

```

There is also a way to query the type of a field.

```julia
julia> struct Foo
           a::Int
           b::Float64
           c::Vector{Union{Missing, Float64}}
       end

julia> fieldtype(Foo, :c)
Vector{Union{Missing, Float64}} (alias for Array{Union{Missing, Float64}, 1})

```

---

<div class="post-metadata">

**Author:** ![Ross\_Boylan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ross_boylan/32/9210_2.png) [@Ross\_Boylan](https://discourse.julialang.org/u/Ross_Boylan)\
**Post date:** [June 22, 2023, 2:05am UTC](https://discourse.julialang.org/t/avoiding-repeated-type-declaration/100679/3 "2023-06-22T02:05:58Z")

</div>

Perfect! Thanks very much.

I was surprised to find that `fieldtype()` can even be used where a type would go, e.g.,

```julia
struct foo; i::Int; end
struct foo2; j::fieldtype(foo, :i); end

```

Seems to work to give `j` type `Int64`.
