Skip to content

Instantly share code, notes, and snippets.

@Beyarz
Created April 7, 2021 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beyarz/4cec26341fb1b5d101a2fe2da4120cc1 to your computer and use it in GitHub Desktop.
Save Beyarz/4cec26341fb1b5d101a2fe2da4120cc1 to your computer and use it in GitHub Desktop.
scope.d
import std.stdio : writefln, writeln;
void main()
{
writeln("<html>");
scope(exit) writeln("</html>");
{
writeln("\t<head>");
scope(exit) writeln("\t</head>");
"\t<title>%s</title>".writefln("Hello");
} // the scope(exit) on the previous line
// is executed here
writeln("\t<body>");
scope(exit) writeln("\t</body>");
writeln("\t\t<h1>Hello World!</h1>");
// scope guards allow placing allocations
// and their clean up code next to each
// other
import core.stdc.stdlib : free, malloc;
int* p = cast(int*) malloc(int.sizeof);
scope(exit) free(p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment