# Why does this conversion happen implicitly?

**URL:** <https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290>\
**Category:** New to Julia\
**Tags:** type, dictionary, convert\
**Created:** [August 4, 2020, 10:28pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290 "2020-08-04T22:28:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [August 4, 2020, 10:28pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/1 "2020-08-04T22:28:26Z")

</div>

```julia
julia> d = Dict{String,Vector{Int}}()
Dict{String,Array{Int64,1}}()

julia> v = view(zeros(Int, 10), :)
10-element view(::Array{Int64,1}, :) with eltype Int64:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

julia> d["as"] = v

julia> d["as"]
10-element Array{Int64,1}:
 0
 0
 0
 0
 0
 0
 0
 0
 0
 0

```

I tripped up today because I had set the wrong values type on my dictionary, so some views were implicitly getting copied upon assignment. Why does this happen? And I don’t mean why in terms of code machinery, but why was it designed this way? I feel like the dict `setindex` method should accept right hand sides that are strict subtypes of the key type, rather than converting - conversion should be decided explicitly by the user.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 4, 2020, 10:38pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/2 "2020-08-04T22:38:05Z")

</div>

Consider:

```julia
julia> d = Dict{String,Float64}()
Dict{String,Float64} with 0 entries

julia> d["foo"] = 123
123

julia> d
Dict{String,Float64} with 1 entry:
  "foo" => 123.0

```

People love the convenience of this. Automatic conversion when values are assigned to typed locations is one of the things that makes Julia feel like a scripting language rather a fussy static language where types have to match exactly or an error gets thrown. You’ve almost certainly relied on this far more often than you realize. It is potentially problematic, however, when it ends up converting and therefore copying mutable values like arrays.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [August 5, 2020, 2:05am UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/3 "2020-08-05T02:05:56Z")

</div>

> [@Benjia](#):
>
> `d["as"]`

You meant

```julia
d["as"] = v

```

right?

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [August 5, 2020, 2:35am UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/4 "2020-08-05T02:35:35Z")

</div>

> [@Benjia](#):
>
> I feel like the dict `setindex` method should accept right hand sides that are strict subtypes of the key type, rather than converting

`view` creates a `SubArray` and `SubArray` is not a subtype of `Array`. There are no subtypes of concrete types in Julia.

---

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [August 5, 2020, 2:10pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/5 "2020-08-05T14:10:56Z")

</div>

My bad - I accidently ommited the key line! Corrected original post.

---

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [August 5, 2020, 2:12pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/6 "2020-08-05T14:12:59Z")

</div>

Exactly. I had initially expected this to error, because SubArray is not a subtype of Array, but instead Dict converts both the key and value types (see for example [https://github.com/JuliaLang/julia/blob/master/base/dict.jl#L381](https://github.com/JuliaLang/julia/blob/master/base/dict.jl#L381)).

---

<div class="post-metadata">

**Author:** ![Benjia](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Benjia](https://discourse.julialang.org/u/Benjia)\
**Post date:** [August 5, 2020, 2:15pm UTC](https://discourse.julialang.org/t/why-does-this-conversion-happen-implicitly/44290/7 "2020-08-05T14:15:07Z")

</div>

Thanks Stefan - this implicit behaviour isn’t entirely to my taste, but I understand why it can be nice in some cases.
