How to use WeakKeyDict


#1

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?


#2

Sometimes you can get some usage examples by looking in the tests. But you should provide more of your code if you want more focussed help…


#3

Thank you for your answer!!!


#4

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> 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