Created
April 7, 2021 16:26
-
-
Save Beyarz/4cec26341fb1b5d101a2fe2da4120cc1 to your computer and use it in GitHub Desktop.
scope.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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