# Constrain type to be concrete subtype

**URL:** <https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207>\
**Category:** General Usage\
**Tags:** type, parametric-types\
**Created:** [October 24, 2022, 7:24pm UTC](https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207 "2022-10-24T19:24:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 24, 2022, 7:24pm UTC](https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207/1 "2022-10-24T19:24:09Z")

</div>

Suppose I have the following struct:

```julia
struct Foo{T <: Real}
    a::T
end

```

I am still able to construct a `Foo` as: `Foo{Real}(20)`. Is there a way to demand that the type parameter be a concrete type such as `Int64`, `Float64`, etc.

I know I could use `isconcretetype` in a constructor to meet that demand, but is that the only way. Is there any way to do it directly in the constraint of the type parameter?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 24, 2022, 8:25pm UTC](https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207/2 "2022-10-24T20:25:33Z")

</div>

No, since every type is also its own subtype. Dispatching on whether or not something is concrete is not possible.

Checking and throwing in the constructor is the only solution I know of, but usually not necessary.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 24, 2022, 8:43pm UTC](https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207/3 "2022-10-24T20:43:06Z")

</div>

Supplying a type parameter is something you normally do only if you want a different type than that of the input value, like e.g. an abstract supertype.

You can instead just call the constructor like `Foo(20)`, and that will return a `Foo{Int}`.

---

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 24, 2022, 8:50pm UTC](https://discourse.julialang.org/t/constrain-type-to-be-concrete-subtype/89207/4 "2022-10-24T20:50:12Z")

</div>

I understand that, I just wanted to make it an error to try to do so. So that I and others couldn’t make a mistake that could lead to a runtime error (methods only defined for concrete types later on somewhere else) or performance issues.

But using a constructor for such validation is fine for my use case. I was just asking if there was a different way out of curiosity.
