[Pharo-project] Adding a message for handling disposable resources?
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(); } What about adding a polimorphic (for example) #releaseAfter: method like *Object>>releaseAfter: aBlock self retain. [ aBlock cull: self ] ensure: [ self dispose ] Object>>dispose "hook for disposable resources" ** Object>>retain "hook for resources which can be retained"* * Alien>>dispose self free **File>>retain self open* * File>>dispose self close Stream>>dispose self close Transaction>>dispose self isOpen ifTrue: [ self rollback ] * And so? this way resources can be handled in an uniform way. Cheers, Guille
Am 29.01.2012 22:01 schrieb "Guillermo Polito" <guillermopolito@gmail.com>:
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(); }
What about adding a polimorphic (for example) #releaseAfter: method like
Object>>releaseAfter: aBlock self retain. [ aBlock cull: self ] ensure: [ self dispose ]
Object>>dispose "hook for disposable resources"
Object>>retain "hook for resources which can be retained"
Alien>>dispose self free
File>>retain self open
File>>dispose self close
Stream>>dispose self close
Transaction>>dispose self isOpen ifTrue: [ self rollback ]
And so? this way resources can be handled in an uniform way.
Cheers, Guille
The source editor in Pharo should have a "appreciated mode" or "ensure quality patterns" - automatically suggesting code templates that improve code quality and depreciate unwanted code structures, depreciated classes, methods. That should automatically teach beginners, bad "procedural" thinkers among Smalltalk programmers. Easy to implement, this. regards, Guido Stepken
participants (2)
-
Guido Stepken -
Guillermo Polito