So in order to have complete automatic resource management you need to build a custom OS and possibly even custom hardware.
// try, catch, error handling, etc.
// omitted for clarity
friendly_write(file,data) {
open(file)
write(file, data)
close(file)
}
It would be an engineering tradeoff of code brevity for possibly lower IO performance. Sometimes it might be worth it. Other's it might not. Java's design reflects the engineering opinion in favor of finer grained control. This is consistent with it's heritage in the line of Algol inspired languages.For a very small benefit of "automatic closing of resources" this looks like a huge trade-off.
And it's easy to determine when the use of the resource is complete: when there are no active pointers to the resource (the circular reference problem I don't think can happen with external resources like files).
with f = open("data.txt")
print f.readline()
The file is automatically closed at the end of the block. Those languages also have explicit open/close/seek calls to let the developer implement more complex cases.Btw, the os closes any open file when the program ends.