Skip to content

Commit

Permalink
Example of finalizer registered within constructor (#36118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzaffalon committed Jun 11, 2020
1 parent 38f2c59 commit f0b369c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions base/gcutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ finalizer(my_mutable_struct) do x
ccall(:jl_safe_printf, Cvoid, (Cstring, Cstring), "Finalizing %s.", repr(x))
end
```
A finalizer may be registered at object construction. In the following example note that
we implicitly rely on the finalizer returning the newly created mutable struct `x`.
# Example
```julia
mutable struct MyMutableStruct
bar
function MyMutableStruct(bar)
x = new(bar)
f(t) = @async println("Finalizing \$t.")
finalizer(f, x)
end
end
```
"""
function finalizer(@nospecialize(f), @nospecialize(o))
if !ismutable(o)
Expand Down

0 comments on commit f0b369c

Please sign in to comment.