Default constructor more strict for parametric type?


#1

Why can’t B accept two Int arguments, when A can? The answer is that a method A(a,b) exists, but a method B(a::T, b) where T<:Real does not exist. Is there a good reason for that? I have some code where I have many members of a few different types, and I don’t want to write these methods by hand, has anybody made a macro that creates the wider signature method?

julia> struct A
       a::Float64
       b::Float64
       end

julia> A(1,1)
A(1.0, 1.0)

julia> struct B{T<:Real}
       a::T
       b::Float64
       end

julia> B(1,1)
ERROR: MethodError: no method matching B(::Int64, ::Int64)
Closest candidates are:
  B(::T<:Real, ::Float64) where T<:Real at REPL[3]:2

#2

https://github.com/JuliaLang/julia/issues/17186