Skip to content

Feature Request: Have a way to specialize on Type without making the argument restricted to Types only #45266

@schlichtanders

Description

@schlichtanders

Because of this current julia bug #23618 it is often needed to explicitly demand for argument specialization for types like myfunc(thisshouldspecializeontypes::Type{T}) where T. The documentation states this clearly.

However this approach makes the argument only dispatch on Types. It would be very helpful to allow for marking an argument to also specialize on types, while still staying a generic argument.

There is currently no way to do so, however the current workaround is code duplication (or the use of a macro respectively). You need to create one function clause for the ::Type{T} case and one for the generic argument. Of course, all code needs to be duplicated, which relies on the argument specialization for types.

As an alternative to this code duplication, I want to suggest adding a macro similar to @nospecialize, named e.g. @specializetype which can be simply prepended to the respective argument and makes it work like a normal argument, but with specialization on types.

On discourse, some have responded that there wouldn't be a need for a generic argument which specializes on Types. I strongly disagree. Because of the above bug, any closure which may refer to a Type needs explicit specialization as of now. The use of closures for me is generic coding and should not require a special case for types.

julia version 1.7.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions