# Constructor bug, what am I missing?

**URL:** <https://discourse.julialang.org/t/constructor-bug-what-am-i-missing/78723>\
**Category:** New to Julia\
**Created:** [March 30, 2022, 7:18am UTC](https://discourse.julialang.org/t/constructor-bug-what-am-i-missing/78723 "2022-03-30T07:18:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 30, 2022, 7:18am UTC](https://discourse.julialang.org/t/constructor-bug-what-am-i-missing/78723/1 "2022-03-30T07:18:21Z")

</div>

This construct bug is so strange, I can’t find where I am wrong.

```julia
struct tcg{F,T}
    a::F
    b::T
    tcg(b::T,a::F=nothing) where {F,T}=new{F,T}(a,b)
    tcg(a::F,b::T) where {F,T}=new{F,T}(a,b)
end

```

```julia
tcg(10)

```

The outcome should be this?

```julia
tcg{Nothing,Int64}(nothing,10)

```

But acctually, it is:

```julia
tcg{Int64, Nothing}(10, nothing)

```

Where am I going wrong?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 30, 2022, 7:44am UTC](https://discourse.julialang.org/t/constructor-bug-what-am-i-missing/78723/2 "2022-03-30T07:44:35Z")

</div>

Case 1:

```julia
struct tcg{F,T}
    a::F
    b::T
    #= tcg(b::T,a::F=nothing) where {F, T} = begin
        @show :tcg1
        new{F,T}(a,b)
    end =#
    tcg(a::F,b::T) where {F, T} = begin
        @show :tcg2
        new{F,T}(a,b)
    end
end

tcg(10)

```

yields the expected

```julia
ERROR: MethodError: no method matching tcg(::Int64)

```

Case 2:

```julia
struct tcg{F,T}
    a::F
    b::T
    tcg(b::T,a::F=nothing) where {F, T} = begin
        @show :tcg1
        new{F,T}(a,b)
    end
    tcg(a::F,b::T) where {F, T} = begin
        @show :tcg2
        new{F,T}(a,b)
    end
end

tcg(10)

```

yields

```julia
:tcg2 = :tcg2

```

This seems to be a worthy new [issue](https://github.com/JuliaLang/julia/issues).

To workaround the problem for the time being you could use

```julia
struct tcg{F,T}
    a::F
    b::T
    tcg(b::T,a::Nothing) where {T} = begin
        @show :tcg1
        new{Nothing,T}(a,b)
    end
    tcg(a::F,b::T) where {F, T} = begin
        @show :tcg2
        new{F,T}(a,b)
    end
end

tcg(10)

```

yielding

```julia
:tcg1 = :tcg1

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [March 30, 2022, 8:12am UTC](https://discourse.julialang.org/t/constructor-bug-what-am-i-missing/78723/3 "2022-03-30T08:12:49Z")

</div>

This is documented and intentional behaviour: [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/#Note-on-Optional-and-keyword-Arguments)

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

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

```

In your case, your constructors are equivalent to this:

```julia
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:

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

```
