nim-lang / RFCs

A repository for your Nim proposals.
136 stars 23 forks source link

Add destructors to standard library objects #534

Open raynei86 opened 1 year ago

raynei86 commented 1 year ago

Abstract

ARC/ORC is already the default memory management technique on latest stable release, but the standard library hasn't followed and utilize destructors where plausible.

Motivation

Many standard library objects and containers still require close() to be called manually. This is mostly alleviated with defer, but you still need to remember to close it. This also isn't consistent with how some parallelization related modules have a =destroy hook.

Description

Provide destructors for standard library modules so cleanup is done automatically. This should also simplify many parsers in the standard library since they all use std/streams and need to call close(); they also have many short functions that just opens a stream, then closes it two lines afterwards. Having a defer there breaks the succinct flow.

Code Examples

No response

Backwards Compatibility

No response

Araq commented 1 year ago

Calling close explicitly will continue to not be an error so that it's still easy to write code that works with the older GCs. No conditional compilation with when required.