# Concrete typing for Type{T} struct fields

**URL:** <https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825>\
**Category:** General Usage\
**Tags:** type, struct, type-stability\
**Created:** [December 30, 2021, 5:57pm UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825 "2021-12-30T17:57:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [December 30, 2021, 5:57pm UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825/1 "2021-12-30T17:57:04Z")

</div>

Dear all,

I would like to create a struct that holds a subtype of `Real`, which I want to use later on to create buffer arrays. I managed to do that in the MWE below, but it seems like the corresponding struct is not a concrete type. Is there anything I can do to alleviate that?

```julia
struct TypeHolder{T<:Real}
	type ::	Type{T}
	function TypeHolder(type::Type{T}) where {T<:Real}
		return new{T}(type)
	end
end
holder = TypeHolder(Float16) #TypeHolder{Float16}
isconcretetype(holder) #false

```

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [December 30, 2021, 6:44pm UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825/2 "2021-12-30T18:44:30Z")

</div>

I get an error with your code because your inner constructor is called `TypeHolder2` instead of `TypeHolder`. If I rename it, then I get what you want if you pass the _type_ of `holder` to `isconcretetype`:

```julia
julia> isconcretetype(typeof(holder)) # true

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [December 30, 2021, 6:47pm UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825/3 "2021-12-30T18:47:38Z")

</div>

First, you should use `isconcretetype` on _types_ such as `TypeHolder{Float16}`, not _values_ (such as `holder` in your example):

```julia
isconcretetype(typeof(holder)) # true

```

but then this does not give you much information: by definition of what a concrete type is, the type of `holder` will always be concrete (since there exists a value, `holder`, having this type).

  

I’m actually not sure of what exactly you’re trying to achieve here, but there’s probably no need to store the type as a field in the struct: having it as a type parameter makes it possible to retrieve it when you need it. A minimal example could look like:

```julia
struct TypeHolder{T<:Real}
    function TypeHolder(::Type{T}) where {T<:Real}
	return new{T}()
    end
end
holder = TypeHolder(Float16) # TypeHolder{Float16}
isconcretetype(typeof(holder)) # true (by definition)

# a function that needs to know about T in order to build a buffer
function compute(x::TypeHolder{T}) where {T}
    buffer = Vector{T}(undef, 10)
    do_something_with(buffer)
end

```

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [December 31, 2021, 10:03am UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825/4 "2021-12-31T10:03:08Z")

</div>

Thank you for the answer and explanation, seems like it should be optimized already then.

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [December 31, 2021, 10:03am UTC](https://discourse.julialang.org/t/concrete-typing-for-type-t-struct-fields/73825/5 "2021-12-31T10:03:16Z")

</div>

Thank you!
