Constructor bug, what am I missing?

This is documented and intentional behaviour: Methods · The Julia Language

Basically f(x, y=default) = ... is just shorthand for two method definitions:

f(x, y) = ...
f(x) = f(x, default)

In your case, your constructors are equivalent to this:

tcg(b::T, a::F) where {F,T} = new{F,T}(a,b)
tcg(b::T) where {F,T} = tcg(b, nothing)
tcg(a::F, b::T) where {F,T} = new{F,T}(a,b)

and since F and T are unconstrained, the third method overwrites the first one, so that tcg(b::T) calls the third method.

I think you probably want these two constructors:

tcg(b) = tcg(nothing, b)
tcg(a::F, b::T) where {F,T} = new{F,T}(a,b)
3 Likes