3 ms·
In C++, proper scoping with RAII (similar to CL's with-blah idioms) would alleviate that issue as do_something in the code below won't compile. struct With
by maximilianburke 15y ago
In C++, proper scoping with RAII (similar to CL's with-blah idioms) would alleviate that issue as do_something in the code below won't compile.
struct WithOpenFile {
FILE *fp;
WithOpenFile(const char *fileName) : fp(fopen(fileName, "w")) {}
~WithOpenFile() { fclose(fp); }
};
int do_something(FILE *f)
{
fprintf(f, "This should work if you know what you're doing");
}
int main(int argc, char *argv[])
{
{
WithOpenFile file("outfile");
}
do_something(file.fp);
return 0;
}
But Lisps will happily accept this:
(defun do-something (foo)
(1+ foo))
(with-open-file (file "outfile")
(do-something file))
- rbanffy 15y agoFine. But now you'll have to accept both files and network sockets. What do you do?
- anonymoushn 15y agoPass the int representing the FD instead of the FILE*?
- rbanffy 15y agoAnd then you are back to dynamic typing again, except that, when the integer you send points to something fprintf doesn't like you'll get a segfault instead of a doesNotUnderstand.