Subtypes (apparently) not defined within module

I don’t understand why my function s1type works as expected but the same code coming from MyType module throws a subtypes not defined error. Any thoughts?

function s1type(T::DataType)
    println("type:\t\t", T)
    println("supertype:\t", supertype(T))
    println("subtypes:\t", subtypes(T))
    return nothing
end

s1type(Real)

module MyType
    export s2type
    function s2type(T::DataType)
        println("type:\t\t", T)
        println("supertype:\t", supertype(T))
        println("subtypes:\t", subtypes(T))
        return nothing
    end
end

using .MyType

s2type(Real) # ERROR: UndefVarError: subtypes not defined

comes from InteractiveUtils, so you need using InteractiveUtils inside the module

btw, this is kinda code smell:

though probably fine for this toy application, but irl you want to either don’t annotate (and throw inside the function dynamically) or use f(::Type{T}) where T to specialize on all types. long story short DataType is a “book keeping” for typing system, we should try not to dispatch with it

1 Like