# Can I mimick finalizing for immutables / is an immutable file handler unfeasible?

**URL:** <https://discourse.julialang.org/t/can-i-mimick-finalizing-for-immutables-is-an-immutable-file-handler-unfeasible/104687>\
**Category:** Internals & Design\
**Tags:** file, finalization, immutable\
**Created:** [October 7, 2023, 3:04am UTC](https://discourse.julialang.org/t/can-i-mimick-finalizing-for-immutables-is-an-immutable-file-handler-unfeasible/104687 "2023-10-07T03:04:52Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 7, 2023, 3:04am UTC](https://discourse.julialang.org/t/can-i-mimick-finalizing-for-immutables-is-an-immutable-file-handler-unfeasible/104687/1 "2023-10-07T03:04:52Z")

</div>

I can’t think of other uses for destructors/finalizers in a language without manual deallocations, so I’m using file-closing as an example. It seems to me that I could implement `open` to open a file and construct an _immutable_ file descriptor, then implement a `close` to use the file descriptor to close the file. However, my concern is that the file descriptor isn’t handled by the garbage collector, so unlike the typical `IOStream`s with `finalizer(close, x)`, the file won’t be automatically closed when the descriptor doesn’t outlive a scope. Granted, I could just construct it again to `close`, but I would need to remember the value. For the sake of the thread, let’s assume an automatic `close` upon deallocation would help because I am forgetful enough to neglect using higher order functions that automatically handle setup and cleanup, like `open`’s other method for the `open`-`close` pattern.

More thought: Immutable semantics allow several unmonitored copies to represent the _same_ instance, which is notably different from typical languages with manual memory management and destructors. Whatever mimicry of automatic finalization cannot execute for each copy, which sounds impossible in the general case for something that isn’t GC-tracked.

---

_[View the full topic](https://discourse.julialang.org/t/can-i-mimick-finalizing-for-immutables-is-an-immutable-file-handler-unfeasible/104687)._
