Skip to content

Conversation

@ringabout
Copy link
Member

@ringabout ringabout commented Oct 23, 2024

proc new*[T](a: var ref T, finalizer: proc (x: ref T) {.nimcall.}) {.
  magic: "NewFinalize", noSideEffect.}

The new function supports a finalizer with a ref T type, which is not compatible with the non-var destructor. Because in a finailizer, users are free to modify the parameter. You cannot wrap it without make it mutable in a non-var destructor, e.g.

proc delete(self: Foo) =
  self.data = 0

new(result, delete)

@Araq
Copy link
Member

Araq commented Oct 23, 2024

I don't understand this. Surely the finalizer can call destroy(x[])?

@ringabout
Copy link
Member Author

ringabout commented Oct 23, 2024

type
  FooObj = object
  Foo = ref FooObj
     data: int

proc delete(self: Foo) =
  self.data = 0

new(result, delete)

the new finalizer creates a wrapper around the delete function

proc deleteHook(self: var FooObj) =
  delete(self)

Then deleteHook is bound to the FooObj. The non-var destructor requires deleteHook to be immutable, i.e., delete to be immutable.

@Araq
Copy link
Member

Araq commented Oct 23, 2024

That only means that new got more picky about the finalizer's requirements, not that it needs to be removed entirely.

@ringabout ringabout assigned ringabout and unassigned ringabout Oct 23, 2024
@ringabout ringabout marked this pull request as draft October 23, 2024 15:18
@arnetheduck
Copy link
Contributor

not that it needs to be removed entirely.

the fact that one call to NewFinalize changes the behavior for all instances of that type is a major wart - getting rid of this atrocity would do the language good.

@Araq
Copy link
Member

Araq commented Oct 23, 2024

the fact that one call to NewFinalize changes the behavior for all instances of that type is a major wart - getting rid of this atrocity would do the language good.

That is not an issue anymore with arc/orc anyway and it's now done consistently. Afaik.

@ringabout
Copy link
Member Author

Succeeded by #24354

@ringabout ringabout closed this Oct 24, 2024
@ringabout ringabout deleted the pr_finalizer_re branch October 24, 2024 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants