[toronto-lisp] Lambda question
tarvydas at visualframeworksinc.com
Sat Oct 16 15:36:43 UTC 2010
The most obvious (to me :-) example is that of callbacks in gui's. It can be quite laborious to specify each callback as a separate function. In fact, the machinery behind my large gui is run by a state machine, where almost every widget/control/pane calls the same entry point (the state machine) and changes only the parameters.
Here's a snippet from that LW gui.
:text "Add Font"
:callback #'(lambda (intf) (transition :call intf 0 0 :func 'change-font)))
The final line is the callback to the state machine.
Further, as Gary mentioned, lambda's are closures which can include (capture) references to surrounding context. For example a lambda can refer to a variable or a slot defined in its parent's scope. When the lisp compiler compiles the lambda, it compiles-in a reference to that variable. The compiled lambda can then be passed as an object (a function object) to any other place and, when the lambda is evaluated, it will correctly refer to the captured variable (even though the parent did not come along for the ride).
So, back to the gui example. In LW, a gui is a class. The class can contain any number of custom slots (e.g. if building an editor, you might include a list of editor buffers as instance variables). The lambda callbacks could refer to such slots as needed.
The overall result is less coding and less complexity and more flexibility.
More information about the toronto-lisp