Is it possible to get an incompletely specified type?

julia> struct A{T}; a::T; end

julia> a1 = A(1)
A{Int64}(1)

julia> a2 = A(1.0)
A{Float64}(1.00000e+00)

julia> typeof(a1) === typeof(a2)
false

Yet, both a1 and a2 are “of type A”. Is it possible to test that?

Ok, see printing - Get simple name of type in Julia? - Stack Overflow.

The solution is to use

julia> name(::Type{T}) where {T} = (isempty(T.parameters) ? T : T.name.wrapper)

julia> name(typeof(a1)) === name(typeof(a2))
true
1 Like

Depending on your underlying use case, the typejoin function may be of interest, which returns “the closest common ancestor of types T and S , i.e. the narrowest type from which they both inherit”, in this case A.

6 Likes

I guess you’re not simply looking for a1 isa A && a2 isa A ? :slight_smile:

Another possibility in this case I think would be nameof(typeof(a1)) == nameof(typeof(a2)).

2 Likes

I think he is and I was going to write the same thing.

No, he is not. He does not know what A is. He knows a1 and a2.

This does look promising. Thanks.

Well, this is new information. But I’m sorry if my phrasing came across as rude.

nameof is going to be highly unreliable since it’s not namespaced at all.

julia> struct A{T}; a::T; end

julia> module Foo
       struct A{T}
           x::T
           y::T
           t::String
           w::Symbol
       end
       end;

julia> nameof(Foo.A{Int}) 
:A

julia> nameof(A{Int})
:A

julia> A{Int} == Foo.A{Int}
false
3 Likes

Would there be a problem when using nameof together with parentmodule?

For example

julia> deparametrized(t) = getproperty(parentmodule(t), nameof(t));

julia> deparametrized(A{Int})
A

julia> deparametrized(Foo.A{Int})
Main.Foo.A

2 Likes