Smalltalk uses closures all over the place. They’re how control structures are built up, for starters. They replace what compound statements do in other languages.
So let’s look at a simple function in C: Read more…
Recently I needed to write a tool to crawl a Mercurial repository and look for certain things in unfinished branches that could cause us problems in the future. Given I knew that Mercurial was written in Python, my first approach to this was to start digging around in its code and see if there was anything in there I could cannibalise to build what I needed. This appeared to be bearing fruit, as I found the ancestor module pretty quickly, but I rapidly realised that in order to do something relatively simple I was going to have to copy vast reams of the Mercurial code to support it.
This is the wrong approach, or at least for most purposes it’s a really bad idea, as there’s a much easier way to write this sort of tool. Read more…
Not being entirely happy with my F# zipper implementation throwing exceptions I decided to investigate whether I could use option types to replace the exceptions thrown when you try to navigate off the tree – for example trying to move up when you are at the top of the tree or move down when you are positioned at a node with no children. The basic implementation is simple enough:
Like many modern languages, Smalltalk has the concept of an exception. When an exception’s signalled, the current stack of contexts – activation frames – is gradually unwound (with unwind blocks – what in Delphi would be called try-finally blocks – being executed at each stage), until a context handles that particular exception.
If no context in the stack handles the Exception, the Exception’s #defaultAction gets called. (How? The first context in the stack tries to find its nextHandlerContext, only it doesn’t have one – it’s nil. UndefinedObject (nil’s class) defines #handleSignal:, which invokes the Exception’s #defaultAction.)
Since Smalltalk has the call stack available for inspection, introspection and general mucking-about, Smalltalk also supports a Exception that can retry the action that signalled the Exception.
You are currently browsing the LShift Ltd. blog archives for January, 2011.