# Struggling with collection types

**URL:** <https://discourse.julialang.org/t/struggling-with-collection-types/54916>\
**Category:** New to Julia\
**Created:** [February 9, 2021, 10:46am UTC](https://discourse.julialang.org/t/struggling-with-collection-types/54916 "2021-02-09T10:46:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 10:46am UTC](https://discourse.julialang.org/t/struggling-with-collection-types/54916/1 "2021-02-09T10:46:52Z")

</div>

Still trying to get my head around the finesses of type hierarchies 🙂

I created the following struct:

```julia
struct Price
    components::Dict{String, Float64}
    precision::Int64
    Price(;precision::Integer) = new(Dict{String, Float64}(), precision)
end

function Price(components::Dict{String, <:Real}; precision::Integer = 2)
    price = Price(precision = precision)

    for entry in keys(components)
        price.components[key] = round(components[key], digits = precision)
    end

    return price
end

Price(components::AbstractVector{Pair{String, <:Real}}; precision::Integer = 2) = Price(Dict{String, Float64}(components), precision = precision)

Base.getindex(price::Price, index:: String) = price.components[index]
Base.setindex!(price::Price, amount::Real, index:: String) = (price.components[index] = round(amount, digits = price.precision))

```

I expected the following call to succeed:

```julia
Price(["c1" => 50, "c2" => 150])

```

But alas, I get the following error:

```julia
ERROR: MethodError: no method matching Price(::Array{Pair{String,Int64},1})
Closest candidates are:
  Price(; precision) at /Users/stef/Programming/Julia Projects/loreco-abm/src/finance/price.jl:7
  Price(::Dict{String,var"#s44"} where var"#s44"<:Real; precision) at /Users/stef/Programming/Julia Projects/loreco-abm/src/finance/price.jl:10
  Price(::AbstractArray{Pair{String,var"#s47"} where var"#s47"<:Real,1}; precision) at /Users/stef/Programming/Julia Projects/loreco-abm/src/finance/price.jl:20
Stacktrace:
 [1] top-level scope at none:1

```

I’m pretty sure I’m overlooking something obvious here but to be honest I have no clue what.

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [February 9, 2021, 10:56am UTC](https://discourse.julialang.org/t/struggling-with-collection-types/54916/2 "2021-02-09T10:56:19Z")

</div>

Hi,

The following works:

```julia
function Price(components::Dict{String, <:Real}; precision::Integer = 2)
    price = Price(precision = precision)

    for entry in keys(components)
        price.components[entry] = round(components[entry], digits = precision)
    end

    return price
end

Price(components::AbstractVector{<:Pair{String, <:Real}}; precision::Integer = 2) = Price(Dict{String, Float64}(components), precision = precision)

```

You used `key` instead of `entry` and your `AbstractVector` needs to be of `<:Pair`s.
