Why does Dynamic Typing really matter?!?

David Eppstein eppstein at ics.uci.edu
Thu Feb 6 10:53:10 EST 2003


In article <623cd325.0302060257.1542e098 at posting.google.com>,
 chastel_pelerin at hotmail.com (Jason Smith) wrote:

> But what I want need is an solution to a problem that would not be
> possible to replicate in a statically typed language...

Take a look at OS X's Cocoa undo manager.

Basically, every time you run a function that you want to be undoable,
you call the undo manager with the function that would be used to undo 
it.  The undo manager remembers these calls and performs them later when 
undos are requested.  It does redos by similarly remembering the calls 
that would be used to undo what happened while it was doing an undo.
In practice this leads to very concise and straightforward looking code 
that handles infinite levels of undo.

This was originally written in Objective C but due to the PyObjC bridge 
can be performed just as well in Python.  Here is a quick cut-down 
example:

class MyDocument:
    def rows(self):
        return self._rows

   def setRows_(self,r):
        undo  = undoManager().prepareWithInvocationTarget_(self)
        undo.setRows_(self._rows)
        self._rows = r

The "prepareWithInvocationTarget_" routine returns a special object 
which remembers the methods called on it and calls them back on its 
argument later.  The return value of prepareWithInvocationTarget_ is a 
value that acts like it has the same type as the argument, in that it 
takes the same method calls, but the behavior is completely different.  
I don't think this could be accomplished in a statically typed language.  
Of course statically typed ui packages still may have undo managers but 
they're more cumbersome.

-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/




More information about the Python-list mailing list