用法:
finalizer(f, x)注册一个函数 f(x) 以在没有 program-accessible 引用 x 时调用,并返回 x 。 x 的类型必须是 mutable struct ,否则此函数的行为是不可预测的。
f 不得导致任务切换,这不包括大多数 I/O 操作,例如 println 。使用 @async 宏(将上下文切换延迟到终结器之外)或 ccall 直接调用 C 中的 IO 函数可能有助于调试目的。
例子
finalizer(my_mutable_struct) do x
    @async println("Finalizing $x.")
end
finalizer(my_mutable_struct) do x
    ccall(:jl_safe_printf, Cvoid, (Cstring, Cstring), "Finalizing %s.", repr(x))
end终结器可以在对象构造时注册。在下面的示例中,请注意我们隐式依赖终结器返回新创建的可变结构 x 。
示例
mutable struct MyMutableStruct
    bar
    function MyMutableStruct(bar)
        x = new(bar)
        f(t) = @async println("Finalizing $t.")
        finalizer(f, x)
    end
end相关用法
- Julia findfirst方法用法及代码示例
- Julia findall方法用法及代码示例
- Julia findmax()用法及代码示例
- Julia findlast方法用法及代码示例
- Julia findnext方法用法及代码示例
- Julia findprev方法用法及代码示例
- Julia findmin()用法及代码示例
- Julia findfirst()用法及代码示例
- Julia findmax用法及代码示例
- Julia findprev()用法及代码示例
- Julia findlast()用法及代码示例
- Julia findmin用法及代码示例
- Julia findnext()用法及代码示例
- Julia fill!用法及代码示例
- Julia first方法用法及代码示例
- Julia firstindex用法及代码示例
- Julia fieldnames用法及代码示例
- Julia filter!用法及代码示例
- Julia fill用法及代码示例
- Julia fieldname用法及代码示例
- Julia fieldtypes用法及代码示例
- Julia first用法及代码示例
- Julia filter用法及代码示例
- Julia first()用法及代码示例
- Julia float方法用法及代码示例
注:本文由纯净天空筛选整理自julialang.org 大神的英文原创作品 Base.finalizer — Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
