Skip to content

Standardize access to type parameters #45376

@Tokazama

Description

@Tokazama

foo(@nospecialize(T::Type{<:Foo})) = T.parameters[1] is helpful when Foo is complicated and could potentially generate excessive methods otherwise (e.g., foo(::Type{<:Foo{T1,T2,...}})). Keno recently pointed out this isn't safe. I propose we standardize access to a types parameters with something like this:

getparam(x::DataType, i::Int) = x.parameters[i]
getparam(x::UnionAll, i::Int) = getparam(x.body, i)
getparam(x::TypeVar, i::Int) = getparam(x.ub, i)
getparam(x::Union, i::Int) = _combine_params(getparam(x.a, i), getparam(x.b, i))
_combine_params(a::Type, b::Type) = typejoin(a, b)
_combine_params(@nospecialize(a), @nospecialize(b)) = a === b ? a : Any

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requeststypes and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions