Anonymous callback procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Anonymous callback procedures

Post by GedB »

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:

Code: Select all

Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
    Result = #PB_ProcessPureBasicEvents 
    ; 
    ; you code here 
    ; 
    ProcedureReturn Result 
EndProcedure 

SetWindowCallback(@MyWindowCallback)
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:

Code: Select all

*MyWindowCallback = Callback(WindowID, Message, wParam, lParam)
    Result = #PB_ProcessPureBasicEvents 
    ; 
    ; you code here 
    ; 
    ProcedureReturn Result 
EndCallback

SetWindowCallback(*MyWindowCallback)
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:

Code: Select all

SetWindowCallback(Callback(WindowID, Message, wParam, lParam))
    Result = #PB_ProcessPureBasicEvents 
    ; 
    ; you code here 
    ; 
    ProcedureReturn Result 
EndCallback
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?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You last suggestion looks like inner class in JAVA for SWING event handling. SetWindowCallback() doesn't requiers only a Procedure pointer, but just an address to the pointer. So you can use a variable to hold the pointer:

Code: Select all


If Test
  *Callback = @Procedure1()
Else
  *Callback = @Procedure2()
EndIf

SetWindowCallBack(*CallBack)

You can even use a label and write your callback in asm if you want :)
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

I was thinking more about creating objects than just windows callback. I used that as an example because it would familiar to more people.

When creating objects a lot of procedures have to be created that are just used to populate the virtual table.

If a library of objects was created then this would mean a lot of procedures in the global namespace.

It would also prevent encapsulation, becuause these function could be called directly.

With an anonymous function objects could be declared with less code and kept private.

I can get the same results by placing the objects in a dll and just exporting the creation functions. However, I would like to do something that will allow static binding.
Post Reply