I apologize in advance for the basic question. I’m trying to write an outer constructor for the following MWE that infers the numeric type T from the arguments. The final constructor fails, because you’re only allowed to exclude trailing type parameters in Julia. What is the right way to add an outer constructor for this type that infers T from the arguments?
struct Foo{T<:Real,N,M}
a::T
b::NTuple{N,T}
Foo{T,N,M}(a::T, b::NTuple{N,T}) where {T<:Real,N,M} = new{T,N,M}(a, b)
end
const Bar{T} = Foo{T,2,3}
# convert input types to T
Foo{T,N,M}(a, b::Tuple{Vararg{<:Number,N}}) where {T<:Real,N,M} = Foo{T,N,M}(T(a), T.(b))
# constant b
Foo{T,N,M}(a, b::Number) where {T<:Real,N,M} = Foo{T,N,M}(a, ntuple(i -> b, N))
# infer T (doesn't work - how to fix)
Foo{N,M}(a, b) where {N,M} = Foo{promote_type(typeof(a), eltype(b)),N,M}(a, b)
I’m also interested in inferring N if b is a Tuple.
Edit: I’ll clarify that I’m looking to be able to use both Foo{2,3}(1.2, 3.4) and Bar(1.2, 3.4). Even if I swap the parameter order (which break other parts of my code) to {N,M,T}, Bar(1.2, 3.4) does not find a defined method.