[Gambas-user] Calling a function via variable name

Tobias Boege taboege at ...626...
Thu Apr 17 12:20:35 CEST 2014


On Wed, 16 Apr 2014, Randall Morgan wrote:
> I'm hoping someone here can help me. I am trying to port a genetic program
> from C to Gambas 3. The original code passes various function as pointers
> in an array. They are then called using the function pointer. How can I
> best pass functions around in Gambas via variable.
> 
> FYI: This program randomly selects a sub set of functions from a list in
> random order and stores them in an array. The array of pointers is then
> updated for the next generation, after testing the solution.
> 

You can emulate functions pointers through objects. Depending on how your
set of functions is structured and how many you have of them, you could do
one of at least two things:

 1) Define a (Create Static) class that represents your function, implement
    the function in its _call() special method, and then you can pass that
    class (which is actually a singleton object) as a surrogate function
    pointer. [ There are other variants of that trick but this one is as
    close to the syntax of function pointers as I could get it. ]

    E.g. you want to pass a function pointer that just prints something,
    then you define your

      --8<--[ PrintFunction.class ]-----------------------------------------
      ' Gambas class file

      Create Static

      Public Sub _call(sMessage As String)
        Print sMessage
      End
      --8<------------------------------------------------------------------

    and use it like

      PassAFunctionPointer(PrintFunction)

    where

      Public Sub PassAFunctionPointer(hFunc As Object)
        hFunc("wherever you get your arguments from")
      End

 2) Define a (Create Static) class that represents your entire set of
    functions. That class implements functions of known names that you can
    later refer to:

      --8<--[ PrintFunctionSet.class ]--------------------------------------
      ' Gambas class file

      Create Static

      Public Sub NormalPrint(sMessage As String)
        Print sMessage
      End

      Public Sub DecoratePrint(sMessage As String)
        Print "[*]";; sMessage
      End
      --8<------------------------------------------------------------------

    You'd use this set like

      PassAFunctionSet(PrintFunctionSet, ["NormalPrint", "DecoratePrint", "DecoratePrint"])

    i.e. you give the set and then a sequence of function names to be
    executed:

      Public Sub PassAFunctionSet(hSet As Object, aFuncs As String[])
        Dim sFunc As String
	Dim iInd As Integer

	For Each sFunc In aFuncs
          Object.Call(hSet, sFunc, ["your argument #" & iInd])
	Next
      End

Of course, if you have lots of functions of similar purpose, you would opt
for the second way because it means less classes and better organisation.

Attached is a project so you can play around.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: function-pointer-0.0.1.tar.gz
Type: application/octet-stream
Size: 4867 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20140417/476fe77e/attachment.obj>


More information about the User mailing list