Skip to content

Commit 5ab47d1

Browse files
authored
Add function-first finalizers (#416)
1 parent 47a5a7a commit 5ab47d1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ Currently, the `@compat` macro supports the following syntaxes:
189189

190190
* `Uninitialized` and `uninitialized` with corresponding `Array` constructors ([#24652]).
191191

192+
* `finalizer` accepts the finalizer to run as the first argument and the object to be finalized
193+
as the second ([#24605])
194+
192195
## Renaming
193196

194197

@@ -363,5 +366,6 @@ includes this fix. Find the minimum version from there.
363366
[#24282]: https://github.com/JuliaLang/julia/issues/24282
364367
[#24372]: https://github.com/JuliaLang/julia/issues/24372
365368
[#24459]: https://github.com/JuliaLang/julia/issues/24459
369+
[#24605]: https://github.com/JuliaLang/julia/issues/24605
366370
[#24652]: https://github.com/JuliaLang/julia/issues/24652
367371
[#24657]: https://github.com/JuliaLang/julia/issues/24657

src/Compat.jl

+8
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,14 @@ end
818818
export Uninitialized, uninitialized
819819
end
820820

821+
if VERSION < v"0.7.0-DEV.2562"
822+
import Base: finalizer
823+
finalizer(f::Function, o) = finalizer(o, f)
824+
finalizer(f::Ptr{Void}, o) = finalizer(o, f)
825+
finalizer(f::Ptr{Void}, o::Ptr{Void}) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)
826+
finalizer(f::Ptr{Void}, o::Function) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)
827+
end
828+
821829
include("deprecated.jl")
822830

823831
end # module Compat

test/runtests.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,6 @@ let a = [1 0 0; 0 1 0; 0 0 1]
900900
@test SparseMatrixCSC{Complex128,Int8}(I, 3, 2)::SparseMatrixCSC{Complex128,Int8} == a[:,1:2]
901901
end
902902

903-
904903
# 0.7.0-DEV.2581
905904
@test isa(Vector(uninitialized, 2), Vector{Any})
906905
@test isa(Vector{Float64}(uninitialized, 2), Vector{Float64})
@@ -909,6 +908,17 @@ end
909908
@test isa(Array{Float64}(uninitialized, 2, 2), Matrix{Float64})
910909
@test isa(Array{Float64,3}(uninitialized, 2, 2, 2), Array{Float64,3})
911910

911+
# 0.7
912+
let A = [1]
913+
local x = 0
914+
finalizer(a->(x+=1), A)
915+
finalize(A)
916+
@test x == 1
917+
A = 0
918+
gc(); gc()
919+
@test x == 1
920+
end
921+
912922
if VERSION < v"0.6.0"
913923
include("deprecated.jl")
914924
end

0 commit comments

Comments
 (0)