Why the inner constructor of a template structure returns "too few type parameters" error?

Ah, I was wrong, I should have tested. You have an ambiguity problem, if both method definitions have all parameters as optional, then Julia is not sure about what should be called with no parameters. I think that what you want is something like:

julia> mutable struct Test{T <:Number}
               μ  ::Union{T,Nothing}
               σ² ::Union{T,Nothing}
               Test(μ::Union{T,Nothing},σ²::Union{T,Nothing}=nothing) where {T} = new{T}(μ,σ²)
               Test(type::Type{T}=Float64) where {T} = new{T}(nothing, nothing)
       end

julia> Test()
Test{Float64}(nothing, nothing)

julia> Test(Int)
Test{Int64}(nothing, nothing)

julia> Test(nothing)
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] Test(::Nothing, ::Nothing) at ./REPL[1]:4 (repeats 2 times)
 [2] top-level scope at REPL[4]:1

julia> Test(1)
Test{Int64}(1, nothing)

So there is no ambiguity.

1 Like