# Loop through subtypes

**URL:** <https://discourse.julialang.org/t/loop-through-subtypes/90634>\
**Category:** New to Julia\
**Created:** [November 22, 2022, 9:47am UTC](https://discourse.julialang.org/t/loop-through-subtypes/90634 "2022-11-22T09:47:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 22, 2022, 9:47am UTC](https://discourse.julialang.org/t/loop-through-subtypes/90634/1 "2022-11-22T09:47:34Z")

</div>

In my package, I have a mother type `T` and subtypes `T1,...,Tn` that all must verify some conditions.

The conditions on these subtypes can easily be expressed as a test :

```julia
@test my_test_function(T1)
@test my_test_function(T2)
...

```

How can I loop through all subtypes of `T` ? Something like :

```julia
for Ti in subtypes(T)
    @test my_test_function(Ti)
end

```

The point is that I dont want to need to change the tests when adding a new one.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [November 22, 2022, 10:16am UTC](https://discourse.julialang.org/t/loop-through-subtypes/90634/2 "2022-11-22T10:16:18Z")

</div>

In what way is `subtypes(T)` insufficient?

Edit: I mean you already provided an implementation, no?

```julia
julia> abstract type Parent end

julia> struct T1 <: Parent end

julia> struct T2 <: Parent end

julia> has_feature(::Type{<:Parent}) = false
has_feature (generic function with 1 method)

julia> has_feature(::Type{T1}) = true
has_feature (generic function with 2 methods)

julia> for Ti in subtypes(Parent)
           @info Ti has_feature(Ti)
       end
┌ Info: T1
└ has_feature(Ti) = true
┌ Info: T2
└ has_feature(Ti) = false

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 22, 2022, 10:36am UTC](https://discourse.julialang.org/t/loop-through-subtypes/90634/3 "2022-11-22T10:36:03Z")

</div>

In the way that I did not try `subtypes` before writing this message…

Thanks a lot, this is exactly what I need.
