# Parameters.jl strange values in struct coming from dict

**URL:** https://discourse.julialang.org/t/parameters-jl-strange-values-in-struct-coming-from-dict/91728
**Category:** General Usage
**Tags:** package
**Created:** [December 16, 2022, 10:56am UTC](https://discourse.julialang.org/t/parameters-jl-strange-values-in-struct-coming-from-dict/91728 "2022-12-16T10:56:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [December 16, 2022, 10:56am UTC](https://discourse.julialang.org/t/parameters-jl-strange-values-in-struct-coming-from-dict/91728/1 "2022-12-16T10:56:29Z")

</div>

I have a struct which I’d like one of the field to come from a dict, so I wrote this code

```julia
using Parameters # v0.12.3

const D1 = Dict(
    "a"=>30,
    "b"=>15,
    "c"=>18,
)

@with_kw mutable struct Struct1
    name::String
    dval ::Int = D1[name]
    Struct1(name) = new(name)
end

```

Now, I initialise my stuct for “a” with

```julia
julia> Struct1("a")
Struct1
  name: String "a"
  dval: Int64 0

```

I’m not sure where zero comes from,

> **if I try a few more times I get**
>
> ```julia
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 5
> 
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 4
> 
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 14
> 
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 0
> 
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 -1152921504606846976
> 
> julia> Struct1("a")
> Struct1
> name: String "a"
> dval: Int64 11088370896
> 
> ```

And checking `D1` makes sense

```julia
julia> D1["a"]
30

```

I think I’m doing something fundamentally wrong, but I’m unsure what? How can I use `D1` to set the `dval`?

Edit1:

This doesn’t seem to be an issue for dict, fixed values are also causing me problems

```julia
julia> @with_kw mutable struct Struct1
           name::String
           dval ::Int = D1[name]
           x::Bool=true
           Struct1(name) = new(name)
       end
Struct1

julia> Struct1("a")
Struct1
  name: String "a"
  dval: Int64 2
  x: Bool false

```

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [December 16, 2022, 11:07am UTC](https://discourse.julialang.org/t/parameters-jl-strange-values-in-struct-coming-from-dict/91728/2 "2022-12-16T11:07:56Z")

</div>

> [@this\_josh](#):
>
> ` Struct1(name) = new(name)`

The inner constructor overrides the default constructor, such as the one created by `@with_kw`, I believe.

Using an outer constructor instead should work:

```julia
@with_kw mutable struct Struct1
    name::String
    dval ::Int = D1[name]
end
 Struct1(name) = Struct1(;name)

```

Note that I have essentially defined an additional constructor, which calls the original one

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [December 16, 2022, 11:14am UTC](https://discourse.julialang.org/t/parameters-jl-strange-values-in-struct-coming-from-dict/91728/3 "2022-12-16T11:14:23Z")

</div>

Yes, it seems that’s correct. Embarrassingly, I think I’ve come across this before. The lack of warning does make it somewhat confusing.
