# How to use WeakKeyDict

**URL:** <https://discourse.julialang.org/t/how-to-use-weakkeydict/5411>\
**Category:** General Usage\
**Created:** [August 16, 2017, 2:26pm UTC](https://discourse.julialang.org/t/how-to-use-weakkeydict/5411 "2017-08-16T14:26:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![usefulhyun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/usefulhyun/32/1770_2.png) [@usefulhyun](https://discourse.julialang.org/u/usefulhyun)\
**Post date:** [August 16, 2017, 2:26pm UTC](https://discourse.julialang.org/t/how-to-use-weakkeydict/5411/1 "2017-08-16T14:26:49Z")

</div>

Hi, There

I want to use WeakKeyDict type  
But, I met a error : objects of type \*\*\* cannot be finalized.  
The only possible type as a key is “string” in my level.  
Does anyone tell me how to use this? and giving an example or link for the tutorial?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 16, 2017, 3:10pm UTC](https://discourse.julialang.org/t/how-to-use-weakkeydict/5411/2 "2017-08-16T15:10:59Z")

</div>

Sometimes you can get some usage examples by looking in the [tests](https://github.com/JuliaLang/julia/blob/master/test/dict.jl). But you should provide more of your code if you want more focussed help…

---

<div class="post-metadata">

**Author:** ![usefulhyun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/usefulhyun/32/1770_2.png) [@usefulhyun](https://discourse.julialang.org/u/usefulhyun)\
**Post date:** [August 17, 2017, 2:29pm UTC](https://discourse.julialang.org/t/how-to-use-weakkeydict/5411/3 "2017-08-17T14:29:46Z")

</div>

Thank you for your answer!!!

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 18, 2017, 2:09pm UTC](https://discourse.julialang.org/t/how-to-use-weakkeydict/5411/4 "2017-08-18T14:09:00Z")

</div>

The tests just test that `WeakKeyDict` behaves like a dictionary. They don’t test the “weak” part of it.

As you already noticed, it does not work with objects that don’t get finalized. Here is an example how you can use it.

```julia
julia> D = WeakKeyDict()
WeakKeyDict{Any,Any} with 0 entries

julia> A = ["sdas"]
1-element Array{String,1}:
 "sdas"

julia> D[A] = 1
1

julia> D
WeakKeyDict{Any,Any} with 1 entry:
  String["sdas"] => 1

julia> A = 1
1

julia> D
WeakKeyDict{Any,Any} with 1 entry:
  String["sdas"] => 1

julia> gc()

julia> D
WeakKeyDict{Any,Any} with 0 entries

```
