-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider finalizer(x,f) to return x rather than nothing #22018
Comments
Also, let's swap the arguments: |
|
Except that we put functions as the first argument everywhere else. |
No. We do that when there's nothing else that has any reason to be in the first position, which is not the case here. A quick search shows other functions where we are not doing that includes |
Also, |
maybe we should add a one-arg form? finalizer(g) = (finalizer(g, finalize); g) |
|
Is there any chance of having type-based finalizers (#1037) rather than per-object ones (by v1.0)? I remember discussions about this in the past, but I don't remember if there was a consensus. |
Proposal from triage:
|
API demo: """
destroy!(x)
Default method for `ondestroy(x)`.
"""
function destroy! end
"""
ondestroy(finalize::Any, @nospecialize x)
Calls `finalize(x)` when no references to `x` exist.
"""
function ondestroy(@nospecialize(finalize), @nospecialize x)
ccall(:jl_register_finalizer, Void, (Any, Any), x, finalize)
return x
end
"""
ondestroy(x)
Calls `destroy!(x)` when no references to `x` exist.
"""
ondestroy(@nospecialize x) = ondestroy(destroy!, x)
@deprecate finalizer(x, f) ondestroy(f, x)
@noinline function finalize(x)
depwarn("Manual execution of all finalizers is deprecated. Use type-based finalizer method `destroy!` instead.`, :finalize)
<original implementation>
end Exploratory usage examples: struct T
fields...
function T(fields...)
return ondestroy(new(fields...)) do
@async println("finalized object")
end
end
T() = ondestroy(new())
Base.destroy!(::T) = @async println("default finalizer called on object")
end
destroy!(T()) Other names considered: |
The originally stated proposal of this issue is non-breaking since it's rather unlikely that anyone would be relying on |
OTOH this is easy to do and why not return the object? |
This is done. |
Currently,
finalizer
returns nothing, which requires simple constructors to be more verbose than necessary:If
finalizer(x, f)
where to return the objectx
instead:I think this would improve readability for simple constructors.
The text was updated successfully, but these errors were encountered: