Defer in D Programming Language
Our example demonstrates the use of scope(exit) in D, which is similar to the defer keyword in other languages. It’s used to ensure that a function call is performed later in a program’s execution, usually for purposes of cleanup.
import std.stdio;
import std.file;
void main()
{
// Immediately after getting a file object with
// createFile, we use scope(exit) to ensure the closing
// of that file. This will be executed at the end
// of the enclosing function (main), after
// writeFile has finished.
auto f = createFile("/tmp/defer.txt");
scope(exit) closeFile(f);
writeFile(f);
}
File createFile(string p)
{
writeln("creating");
auto f = File(p, "w");
return f;
}
void writeFile(File f)
{
writeln("writing");
f.writeln("data");
}
// It's important to check for errors when closing a
// file, even in a scope(exit) block.
void closeFile(File f)
{
writeln("closing");
f.close();
}Running the program confirms that the file is closed after being written.
$ rdmd defer.d
creating
writing
closingIn this D version:
We use
scope(exit)instead ofdefer. This ensures that thecloseFilefunction is called when the current scope (in this case,main) exits.We use D’s
Filetype instead of Go’s*os.File.Error handling is slightly different in D. Instead of using
panic, D would typically use exceptions. However, for simplicity, we’ve omitted explicit error handling in this example.The
createFilefunction returns aFileobject directly, rather than a pointer.In D, we use
autofor type inference where applicable.We use
writelnfromstd.stdioinstead offmt.Println.
This example demonstrates how D’s scope(exit) can be used to ensure cleanup operations are performed, similar to Go’s defer.