Hacker News new | ask | show | jobs
by masklinn 401 days ago
Destructors require deterministic cleanup, which advanced GCs can't do (and really don't want to either from an efficiency perspective). Languages with advanced GCs have "finalizers" called during collection which are thus extremely unreliable (and full of subtle footguns), and are normally only used as a last resort solution for native resources (FFI wrappers).

Hence many either had or ended up growing means of lexical (scope-based) resource cleanup whether,

- HoF-based (smalltalk, haskell, ruby)

- dedicated scope / value hook (python[1], C#, Java)

- callback registration (go, swift)

[1]: Python originally used destructors thanks to a refcounting GC, but the combination of alternate non-refcounted implementations, refcount cycles, and resources like locks not having guards (and not wanting to add those with no clear utility) led to the introduction of context managers

1 comments

What does "HoF" stand for?
higher order function, function taking an other function (/ block).

E.g. in Ruby you can lock/unlock a mutex, but the normal way to do it would be to pass a block to `Mutex#synchronize` which is essentially just

  def synchronize
    lock
    begin
      yield
    ensure
      unlock
    end
  end
and called as:

  lock.synchronize {
    # protected code here
  }