Skip to content

Autogenerate (::Type{Foo})(x::T, y) where T constructor for type Foo{T} #35053

@jakobnissen

Description

@jakobnissen

Consider a definition of a parameter-less struct:

struct Bar
    x::Int
    y::Float64
end

This creates two constructors: A fully specified Bar(x::Int64, y::Float64) and a fully unspecified Bar(x, y). The latter is very convenient because you can just do Bar(1, 1) and it will automatically convert.

For a simple struct with a type param, we have something similar:

struct Baz{T}
    x::T
end

creates the specified (::Type{Baz{T}})(x) where T and the unspecified (::Type{Baz})(x::T) where T. The latter handily allows you to call Baz(1.0) and get exactly what you expect.

However, if you make:

struct Foo{T}
    x::T
    y::Float64
end

You get (::Type{Foo{T}})(x, y) where T and (::Type{Foo})(x::T, y::Float64) where T. But there is no equivalent convenience method (::Type{Foo})(x::T, y) where T that allows you to do Foo(1, 1) and have it work. That would be nice to have though.

This was raised by @arnavs on Slack who found it counterintuitive.

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