Hi!
It's very common to have the same structure when handling resources that should be disposed after using -normally when they belong outside the image- like files, aliens, database transactions, mutex semaphores...
[
open the file
do something with the file
] ensure: [ close the file ]
[
create an alien
do something with the alien
] ensure: [ free the alien ]
[
open the transaction
do something with the transaction
transaction commit
] ensure: [ transaction if open, transacction rollback. ]
Some times, this structure is abstracted in a high order method(?) using blocks..
session inTransactionDo: [ "do something with the transaction" ]
Semaphore forMutualExclusion critical: [
�� "do something with the semaphore
]
Ok, Python and C# add the with and using statement, handling the resource disposing in a polymorphic way block of code is executed, like:
with aFile:
�� #use the file
or
using (aDatabaseTransaction){
� /* use the database transaction */
}
Even in Java 7 they've included a construction for auto-dispose of resources:
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}