module Handles {
// HANDLE BASICS
// The handle type is used to define types for operating system handles. A handle is a nominal
// type, meaning two handle definitions are not considered the same type.
// For example, the standard library defines a File handle type (and associated methods):
//
// typedef File = handle;
fn test1() {
// open file and return handle
let f = File.create("my.txt");
// write some text to file
f.write("hello world");
// close file
f.close();
}
// DEFER
// In the example above, if f.write throws an exception, then f.close will not execute
// and the function will "leak" a handle.
// Use defer to ensure that a statement (or block) runs when the current block exits.
fn test2() {
let f = File.create("hello.txt");
// f.close(); will run at the end of the block
defer f.close();
f.write("hello world");
}
}