Anonymous callback procedures
Posted: Wed Apr 14, 2004 7:52 am
Callbacks are very useful feature, and being able to define an anonymous callback function would make suing them easier and cleaner, especially within libraries. The pointer to the procedure would be assigned to a variable rather than being associated with a procedure name.
At the moment the creation of any callback procedures requrie the use fo a procedure name. This clutter both the code and the global namespace.
For example, at the moment a windows callback would be set as follows:
In the above example the MyWindowCallback procedure would never be referred to again, but it woulld take up that name.
If I had another window with the same callback name I would get a nameclash, making it necessary to use naming conventions if a file is to be reused.
An anonymous callback would allow the following:
This could then be taken one step further.
It could be that a callback declaration could be useed a procedure call (on the same line. In this case a pointer to the callback will be passed to the procedure. The code would look like this:
This feature would also be very useful when creating objects using interfaces.
Anybody who has used Ruby will know just how useful this type of callback can be.
Any thoughts?
At the moment the creation of any callback procedures requrie the use fo a procedure name. This clutter both the code and the global namespace.
For example, at the moment a windows callback would be set as follows:
Code: Select all
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
;
; you code here
;
ProcedureReturn Result
EndProcedure
SetWindowCallback(@MyWindowCallback)
If I had another window with the same callback name I would get a nameclash, making it necessary to use naming conventions if a file is to be reused.
An anonymous callback would allow the following:
Code: Select all
*MyWindowCallback = Callback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
;
; you code here
;
ProcedureReturn Result
EndCallback
SetWindowCallback(*MyWindowCallback)
It could be that a callback declaration could be useed a procedure call (on the same line. In this case a pointer to the callback will be passed to the procedure. The code would look like this:
Code: Select all
SetWindowCallback(Callback(WindowID, Message, wParam, lParam))
Result = #PB_ProcessPureBasicEvents
;
; you code here
;
ProcedureReturn Result
EndCallback
Anybody who has used Ruby will know just how useful this type of callback can be.
Any thoughts?