Outer constructor method in Julia

Hi, I’m a bit confused about the following outer constructor method call.

mutable struct OSMData
        nodes::Dict{Int, OpenStreetMapX.LLA}
        ways::Vector{OpenStreetMapX.Way}
        relations::Vector{OpenStreetMapX.Relation}

        features::Dict{Int, Tuple{String,String}}
        bounds::OpenStreetMapX.Bounds
        
        way_tags::Set{String}
        relation_tags::Set{String}
end
OSMData() = OSMData(Dict{Int, OpenStreetMapX.LLA}(), Vector{OpenStreetMapX.Way}(),
                                       Vector{OpenStreetMapX.Relation}(), Dict{Int, String}(),
                                       Bounds(0.0, 0.0, 0.0, 0.0), Set{String}(), Set{String}())

My question is, for the feature attribute of this outer constructor method, the parameter is
Dict{Int, String}(). On the other hand, the definition of the struct OSMData requires that the type of the features attribute is Dict{Int, Tuple{String,String}}. Can anyone please let me know why it still works out well in this case.

I cannot really tell you why, but there seems to be implicit conversion of empty dicts:

julia> struct Foo
       x :: Dict{Int,Tuple{String,String}}
       end

julia> Foo(Dict())
Foo(Dict{Int64,Tuple{String,String}}())

julia> Foo(Dict{Int, String}())
Foo(Dict{Int64,Tuple{String,String}}())

julia> Foo(Dict{Int, Any}())
Foo(Dict{Int64,Tuple{String,String}}())
1 Like

Unless an inner constructor is added, there is a default one that calls convert to the field type on all the arguments.

6 Likes

still would be more readable to have Dict{Int, Tuple{String,String}}()
also Vector{OpenStreetMapX.Relation}() would look better just as OpenStreetMapX.Relation[]
you can make a pull request and I will merge it