Functions in Type Declarations

You are representing T twice: once in the type domain as a parameter to your struct, and a second redundant time as the child field. The same applies for S in your “workaround”.
If you do it only in the type domain, it’s much simpler, since you can call functions inside the type parameters to new (or any function call):

julia> struct ChildParent{T,S}
           ChildParent(T) = new{T,supertype(T)}()
       end

julia> ChildParent(Int)
ChildParent{Int64,Signed}()

You can then access the T and S “fields” inside functions like this:

function f(cp::ChildParent{T,S}) where {T,S}
    # T and S available here
end
4 Likes