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:
[x] Support let use syntax anywhere that let is currently allowed
[x] Require explicit initialization of variables in let use statements
[x] Require the initializer to be of a type that implements Ghul.Disposable (=System.IDisposable)
[x] Report an error on any attempt to assign to a let use variable after initialization
[x] Wrap the enclosing statement list in a try / finally that ensures all let use variables in that scope are disposed even if an exception is thrown before the end of the block is reached
[x] Call dispose() in reverse order of declaration
[x] Skip calling dispose() on any let use variable of reference type that is null
Support guaranteed disposal of resources referenced by local variables when those variables go out of scope.
Although
try
/finally
provides the building blocks needed to write code that is guaranteed to calldispose()
on managed resources in the face of possible exceptions, it's clunky, and it can require nestedtry
/finally
blocks to implement correctly if the resource acquisition itself could throw. Thelet use
statement will provide a more concise and less error prone alternative.Requirements:
let use
syntax anywhere thatlet
is currently allowedlet use
statementsGhul.Disposable
(=System.IDisposable
)let use
variable after initializationlet use
variables in that scope are disposed even if an exception is thrown before the end of the block is reacheddispose()
in reverse order of declarationdispose()
on anylet use
variable of reference type that is null