nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.64k stars 1.47k forks source link

deprecates `NewFinalize` which is incompatible with non-var destructor #24350

Closed ringabout closed 1 month ago

ringabout commented 1 month ago
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 commented 1 month ago

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

ringabout commented 1 month ago
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 commented 1 month ago

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

arnetheduck commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

Succeeded by https://github.com/nim-lang/Nim/pull/24354