Passing a function as a parameter to another function

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Passing a function as a parameter to another function

Post by tjm167us » Fri Aug 01, 2014 6:34 pm

Hi everyone,
I am interested in passing a function as a condition to a repeat loop within a function I am writing that I want evaluated every time through the loop. In code:

Code: Select all

function myRepeat conditions, maxIterations
   put 1 into loopCounter
   repeat until (conditions or loopCounter>maxIterations)
      wait 200 milliseconds
      add 1 to loopCounter
   end repeat
   return 0
end myRepeat
With the idea that conditions could be something like "the mouseClick". I would then use this function like:

Code: Select all

put myRepeat("the mouseClick",3) into output
What I am really asking is how I can make LiveCode interpret a text string as a function call. Not sure I have ever done this before or if this is even possible!
Thanks,
Tom
Last edited by tjm167us on Fri Aug 01, 2014 7:25 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Passing a function as a parameter to another function

Post by FourthWorld » Fri Aug 01, 2014 7:02 pm

Quoted text will be passed as a literal string. Unquoted text will be evaluated before being passed to the function.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Re: Passing a function as a parameter to another function

Post by tjm167us » Fri Aug 01, 2014 7:21 pm

Hi Richard, thanks for the prompt response. However, I don't think that's quite what I'm looking for. I understand not using the quotes (because I don't want the parameter in the repeat loop to be a string, I want it to be a function). But I also don't want the "mouseClick" function to be evaluated before the function call "myRepeat" is evaluated. I want the function to be evaluated every time through the loop to determine whether I should stop looping. Does this make sense? I can provide more info if necessary!
Thanks,
Tom

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Passing a function as a parameter to another function

Post by FourthWorld » Fri Aug 01, 2014 7:42 pm

Ah, thanks for the clarification. There are probably many ways to solve this, and if I knew more about the context and larger goals of the application I might be able to suggest something better than this, but you can use the value function to have a string evaluated, e.g::

Code: Select all

function myRepeat condition, maxIterations
   put 1 into loopCounter
   repeat until ( value(condition) is true or loopCounter>maxIterations)
      wait 200 milliseconds
      add 1 to loopCounter
   end repeat
   return 0
end myRepeat
My reluctance in relying on the value function is the same generic caution I have with the "do" command: while useful in some contexts, there's usually a more efficient and less error-prone way to handle things (open-ended runtime evaluation can be very tricky to debug if anything goes wrong). But both of those exist in the language for a reason, much as JavaScript has its eval function, and when you need 'em you need 'em.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply