degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

Scoped resource disposal #1090

Closed degory closed 9 months ago

degory commented 9 months ago

Support guaranteed disposal of resources referenced by local variables when those variables go out of scope.

example() is
    ... other code

    let use file_stream = IO.File.open_read("test.txt");
    let use text_reader = new IO.StreamReader(file_stream);

    ... other code

    // text_reader.dispose() automatically called here
    // file_stream.dispose() automatically called here
si

Although try / finally provides the building blocks needed to write code that is guaranteed to call dispose() on managed resources in the face of possible exceptions, it's clunky, and it can require nested try / finally blocks to implement correctly if the resource acquisition itself could throw. The let use statement will provide a more concise and less error prone alternative.

Requirements: