[self-interest] simplicity

Chandrasekhar Ramakrishnan cramakrishnan at acm.org
Sun Jun 27 19:02:00 UTC 2004


Ian Woollard schrieb:
> For example in C++ you can do things like:
> 
> {
>      FileHandle f = open("/adirectory/aFile", open);
> 
>     f << "I went to the zoo";
> }
> 
> And the file may be automagically closed as f goes out of scope.

Yes, but you could also do this:

class ZooWriter {
	FileHandle mFH;
public:
	void write_zoo() {
		mFH = open("/adir/afile", open);
		mFH << "I went to the zoo";
	}

	void after_writing_zoo_write_home() {
		mFH << ". Then I went home";
	}

	void essay() {
		write_zoo();
		after_writing_zoo_write_home();
	}
}

And then you'll have a problem.  

(Aside -- how would you implement the open function described in your
example? Instead of creating an open function, I would have given
FileHandle a two argument constructor and written FileHandle f("path",
open), but there's always more I can learn about C++.)

> But this is unreliable in Java/Self/Smalltalk- you don't know when the 
> GC will get around to release it. I consider this to be a bug in these 
> languages.

Yes, but if your runtime uses a two-space GC implementation and
collects recently allocated objects more frequently, the file handle
will be closed reasonably soon, and you get the added bonus that the
example I provided won't cause any problems.

- sekhar

--
C. Ramakrishnan        cramakrishnan at acm.org



More information about the Self-interest mailing list