onyx-lang / onyx

✨ The compiler and developer toolchain for Onyx
https://onyxlang.io
BSD 2-Clause "Simplified" License
570 stars 20 forks source link

Feature: Auto dispose locals #130

Closed brendanfh closed 6 months ago

brendanfh commented 6 months ago

This pull requests adds what I am calling 'disposable locals'. The idea is inspired by C#'s using statement, where the programmer can decide to easily, automatically, defer the disposable of a local variable using the use keyword before the declaration. It abstracts the pattern of,

value := create_a_thing();
defer delete_a_thing();

to,

use value := create_a_thing();

While it might seem like a needless complexity, and it arguably is, I want to try it out to see if it leads to cleaner and more readable Onyx code. The large learning curve is figuring out the things that need to be used, but this issue also exists in C#. It is quite easy to forget that something needs to be disposed of in that language.

If someone does not want to use the feature, they are welcome to stick to the explicit defer.