From f0b369cca94109a73751c671900357eccefe9e65 Mon Sep 17 00:00:00 2001 From: Michele Zaffalon Date: Thu, 11 Jun 2020 16:24:38 +0200 Subject: [PATCH] Example of finalizer registered within constructor (#36118) --- base/gcutils.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/base/gcutils.jl b/base/gcutils.jl index 23ba4a82b65b5..c403e4f20f626 100644 --- a/base/gcutils.jl +++ b/base/gcutils.jl @@ -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)