by taeber · 4y ago · 0 comments
I previously shared a "with" statement idea on HN that I thought fit with the rest of the language.
with (FILE *fp; fp = fopen(path, "wb"); fclose(fp)) {
...
} else {
perror("Failed to open file");
}
https://github.com/taeber/cwithhttps://news.ycombinator.com/item?id=23370693
edit: formatting
for(FILE *fp = fopen(path, "wb");
fp != NULL;
fclose(fp), fp = NULL) {
...
}If you are interested, I did provide a few macros[2], but as another commenter pointed out, they won't handle an early return. You can use "break" to exit early though:
#define with(declare, startup, cleanup, block) \
{ \
declare; \
if (( startup )) { \
do { \
block; \
} while(0); \
cleanup; \
} \
}
with (FILE *fp, fp = fopen(path, "rb"), fclose(fp), {
fread(buf, 1, sizeof(buf)-1, fp);
if (ferror(fp)) {
perror(path);
break;
}
printf("%s\n", buf);
success = 1;
})
There's also "withif" #define withif(declare, startup, cleanup, block, otherwise) \
{ \
declare; \
if (( startup )) { \
do { \
block; \
} while(0); \
cleanup; \
} otherwise; \
}
[1]https://github.com/taeber/cwithHere is a macro that can be used to emulate a defer
#define CONCAT_(a, b) a ## b
#define CONCAT(a, b) CONCAT_(a, b)
#define UNIQUENAME() CONCAT(i_, __LINE__)
#define SCOPE_(counter, init_stmt, exit_stmt) for (int counter = ((init_stmt), 1); counter--; (exit_stmt))
#define SCOPE(init_stmt, exit_stmt) SCOPE_(UNIQUENAME(), (init_stmt), (exit_stmt))
Granted it's a hack, but it can be useful at times. I've used something like it to define a large data hierarchy in code for example, as having to close all the nodes manually is tedious.You could wrap a second for-loop around the definition of the macro to at least be able to catch misplaced "break" statements.