[Gambas-user] Calling a function via variable name

Randall Morgan rmorgan62 at ...626...
Thu Apr 17 12:44:10 CEST 2014


Thanks Tobi,

I stumbled onto the idea in  #2 myself. I also thought about creating an
Gene class that uses and index value to call one of it's many methods. The
methods being encoded in the dna array.

Thanks for the ideas and the code!


On Thu, Apr 17, 2014 at 3:20 AM, Tobias Boege <taboege at ...626...> wrote:

> 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
>
>
> ------------------------------------------------------------------------------
> Learn Graph Databases - Download FREE O'Reilly Book
> "Graph Databases" is the definitive new guide to graph databases and their
> applications. Written by three acclaimed leaders in the field,
> this first edition is now available. Download your free book today!
> http://p.sf.net/sfu/NeoTech
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>


-- 
If you ask me if it can be done. The answer is YES, it can always be done.
The correct questions however are... What will it cost, and how long will
it take?



More information about the User mailing list