Convincing Programmers They Want To Use Lisp
pschombe.wordpress.com
pschombe.wordpress.com
C# has the IDisposable + using construct for exactly this purpose. If you're not happy with that you can write your own using a lambda, just like his example...
using (open file here)
{
// Do work here
}
WithNetFile(f => do work here);
Lisp is neat, this is not a great argument though.The author makes this clear later in the post: "Once you have caught the programmer’s interest with simple macros show them some other cool things they can do, like language extensions. Experienced programmers are always looking for more control, and macros give it to them."
For example I can write:
{
ifstream file("filename");
...
} // file closed here automatically.
or {
mutex_grab g(mutex);
.....
} // mutex automatically released here doWithStreamThenClose: aBlock
| fileStream |
fileStream := self newReadWriteStream.
^[aBlock value: fileStream]
ensure: [fileStream close]
This allows one to do: (Filename named: 'mypath/foo.dat')
doWithStreamThenClose: [ :fileStream |
"do whatever you want with fileStream"
"it shall be closed"
].For most programmers, they probably need at least either Java or C#, a scripting language (Ruby, Perl, or Python), and a Lisp language (Clojure, Common Lisp, or a Scheme implementation).
Wow.
Is it any wonder that you cannot convince someone to use Lisp, when you have no idea what other languages actually have to offer?
Python 2.6 has a built-in "with" construct for this. Java, C#, earlier versions of Python and (gasp!) VB have the try, catch, finally construct. Offhand, I can't think of a language that can't do this, but I'm sure they exist.
Seems pretty clear he's talking about the opening/creating and closing/destroying resources.