-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
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.