Skip to content

Commit 4635377

Browse files
authored
make WeakKeyDict more type stable and add const to some fields (#57877)
1 parent 97a2403 commit 4635377

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

base/weakkeydict.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
# weak key dictionaries
44

5+
mutable struct WeakKeyDictFinalizer{T}
6+
const d::T
7+
end
8+
(d::WeakKeyDictFinalizer)(k) = d.d.dirty = true
9+
10+
511
"""
612
WeakKeyDict([itr])
713
@@ -16,15 +22,15 @@ object was unreferenced anywhere before insertion.
1622
See also [`WeakRef`](@ref).
1723
"""
1824
mutable struct WeakKeyDict{K,V} <: AbstractDict{K,V}
19-
ht::Dict{WeakRef,V}
20-
lock::ReentrantLock
21-
finalizer::Function
25+
const ht::Dict{WeakRef,V}
26+
const lock::ReentrantLock
2227
dirty::Bool
28+
finalizer::WeakKeyDictFinalizer
2329

2430
# Constructors mirror Dict's
25-
function WeakKeyDict{K,V}() where V where K
26-
t = new(Dict{WeakRef,V}(), ReentrantLock(), identity, 0)
27-
t.finalizer = k -> t.dirty = true
31+
function WeakKeyDict{K,V}() where {K, V}
32+
t = new{K,V}(Dict{WeakRef,V}(), ReentrantLock(), false)
33+
t.finalizer = WeakKeyDictFinalizer(t)
2834
return t
2935
end
3036
end

0 commit comments

Comments
 (0)