20. September 2009 13:58
One of my favorite features from when I was playing around with F# was high order functions. A high order function is quite simply a function that takes a function as an argument and returnes a function. A basic sample just to get the point accross could be:
innerText = "Hey dude! "
print innerText + text
myfunc = Outer()
myfunc("My function was called successfully")
This technique can be quite useful when composing functions, but also to wrap code and handle cross cutting concerns (e.g. like logging) through Aspect Oriented Programming. IronPython takes this concept a bit farther through what is called decorators. A decorator is to some extent similar to an attribute, and can be used to do AOP. Using decorators is as simple as defining a high order function and decorating the method that should be wrapped with @myHighOrderFunction like this:
print "Do pre call logging"
print "Do post call logging"
print "Could do what interesting stuff you like"
As you can see from the code it makes for a very straight foreward and elegant way og doing AOP.
UPDATE: I stumbled across this video with DevHawk where he actually shows a similar example during his talks here in DK.