# Position of type definition in struct with default values

**URL:** https://discourse.julialang.org/t/position-of-type-definition-in-struct-with-default-values/74909
**Category:** New to Julia
**Tags:** type, struct
**Created:** [January 20, 2022, 6:37am UTC](https://discourse.julialang.org/t/position-of-type-definition-in-struct-with-default-values/74909 "2022-01-20T06:37:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![unis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/unis/32/19458_2.png) [@unis](https://discourse.julialang.org/u/unis)
#### Post date: [January 20, 2022, 6:37am UTC](https://discourse.julialang.org/t/position-of-type-definition-in-struct-with-default-values/74909/1 "2022-01-20T06:37:27Z")

</div>

I’d like to define a struct containing field variables with default values. Am I right in assuming there is no difference between

```julia
import Parameters: @with_kw

@with_kw struct S
       d::Dict{Int,Int} = Dict()
end

```

and

```julia
@with_kw struct T
       d = Dict{Int,Int}()
end

```

?

In both cases, we get

```julia
julia> S()
S
  d: Dict{Int64, Int64}

```

and

```julia
julia> T()
T
  d: Dict{Int64, Int64}

```

, respectively.

Which of the two definitions should be preferred? Thanks a lot!

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [January 20, 2022, 8:45am UTC](https://discourse.julialang.org/t/position-of-type-definition-in-struct-with-default-values/74909/2 "2022-01-20T08:45:26Z")

</div>

The `d` in `T` is not annotated, thus it can be any type according to initialization.

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [January 20, 2022, 8:46am UTC](https://discourse.julialang.org/t/position-of-type-definition-in-struct-with-default-values/74909/3 "2022-01-20T08:46:50Z")

</div>

As you use it there is no difference, but here is an example where you can see how it would matter

```julia
julia> test = Dict(:A => 1, :B => 4)
Dict{Symbol, Int64} with 2 entries:
  :A => 1
  :B => 4

julia> S(test)
ERROR: MethodError: Cannot `convert` an object of type Symbol to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::Ptr) where T<:Integer at ~/git/julia/usr/share/julia/base/pointer.jl:23
  convert(::Type{T}, ::T) where T<:Number at ~/git/julia/usr/share/julia/base/number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at ~/git/julia/usr/share/julia/base/number.jl:7
  ...
Stacktrace:
 [1] setindex!(h::Dict{Int64, Int64}, v0::Int64, key0::Symbol)
   @ Base ./dict.jl:373
 [2] Dict{Int64, Int64}(kv::Dict{Symbol, Int64})
   @ Base ./dict.jl:104
 [3] convert
   @ ./abstractdict.jl:525 [inlined]
 [4] S(d::Dict{Symbol, Int64})
   @ Main ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:505
 [5] top-level scope
   @ REPL[11]:1

julia> T(test)
T
  d: Dict{Symbol, Int64}

```

since `T` will allow any type, just that the default is the same, but `S` will not allow any other type. If you know that you always want the field to be `Dict{Int,Int}` you probably want `S`.
