How to execute the result of a function when...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

How to execute the result of a function when...

Post by melristau » Fri Jul 22, 2016 1:00 am

what is returned by function is the name of a handler?

Code: Select all

on mouseUp
   global gDoThis
   put figureThis(gDoThis) into newTask
   newTask --(the name of a handler. like "doLink")
end mouseUp

on doLink
   beep
end doLink
rebuilding visual programming app originally created in 1993 as Hypercard stack

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

Re: How to execute the result of a function when...

Post by FourthWorld » Fri Jul 22, 2016 2:12 am

You can use the "do" command for that, but as a general rule whenever I find myself tempted to use "do" I take it as an early warning sign that I'm making something harder than it needs to be, and certainly harder to debug and maintain.

What are you doing with these functions that can't be determined in advance? How many are there?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: How to execute the result of a function when...

Post by dunbarx » Fri Jul 22, 2016 6:14 am

Richard.

I have no idea what the OP means. One of you please explain.

Craig Newman

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to execute the result of a function when...

Post by Thierry » Fri Jul 22, 2016 7:49 am

melristau wrote:what is returned by function is the name of a handler?

Code: Select all

on mouseUp
   global gDoThis
   put figureThis(gDoThis) into newTask
   newTask --(the name of a handler. like "doLink")
end mouseUp

on doLink
   beep
end doLink
Hi,

Here is 2 ways to manage this nicely.

Code: Select all

on mouseUp
   dispatch figureThis( "whatever")
end mouseUp

Code: Select all

on mouseUp
   send figureThis( "whatever") to me in 1 millisecond
end mouseUp

Code: Select all

on doLink
   beep
end doLink

function figureThis state
   return "doLink"
end figureThis
If dispatch looks easier, the send command is more powerful as it basically
will make your app more responsive... I personally never use the 'do' command

Check dispatch and send in the dictionary.

HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Fri Jul 22, 2016 7:23 pm

Thierry-

I think the proper dispatch syntax for functions is

Code: Select all

on mouseUp
   dispatch function "figureThis" with "whatever"
end mouseUp
But at any rate I would code this as

Code: Select all

on mouseUp
   global gDoThis
   put figureThis(gDoThis) into newTask
   send newTask to me in 0 milliseconds
end mouseUp

function figureThis
   -- return the name of the handler to call
   -- i.e., return "doLink"
end figureThis

on doLink
   beep
end doLink

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to execute the result of a function when...

Post by Thierry » Fri Jul 22, 2016 8:10 pm

mwieder wrote:Thierry-

I think the proper dispatch syntax for functions is

Code: Select all

on mouseUp
   dispatch function "figureThis" with "whatever"
end mouseUp
Hi Mark,

Thanks for your comments.

As it is a bit late here, I've done a quick test to be completely sure,
and this really works:

Code: Select all

on mouseUp
   dispatch figureThis( "whatever")
end mouseUp

on doLink
   answer "cool!"
end doLink

function figureThis state
   return "doLink"
end figureThis
After all, figureThis( "...") is evaluated before the dispatch,
therefore, a bit similar as writing:

dispatch "dolink"

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Fri Jul 22, 2016 8:30 pm

Thierry-

Yes, it does/should work.

I left it in the long form because it was closer to the OP's original code and therefore the underlying mechanics might be more transparent, but I'd otherwise do much as you did.

I also used the "send" form rather than "dispatch" because I can't remember how asynchronous the dispatch command is, and it might be important to end the mouseUp handler before processing the result of figureThis. For a single-line call like this it shouldn't matter, but if the executing command is blocking or otherwise takes a long time, the end result could be ambiguous.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: How to execute the result of a function when...

Post by [-hh] » Fri Jul 22, 2016 8:43 pm

Very interesting. One thing more:
Recently I learned from Ali@LC that one could add

Code: Select all

call handler [of object]
to your list of commands because "call" doesn't change the context, contrary to "send".
Just in case one needs the target ...
shiftLock happens

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Fri Jul 22, 2016 9:41 pm

Should be noted that the call command only works for other commands, not functions.

Interestingly, I don't see the "dispatch function" form documented in the dictionary.

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

Re: How to execute the result of a function when...

Post by FourthWorld » Sat Jul 23, 2016 2:14 am

mwieder wrote:Should be noted that the call command only works for other commands, not functions.

Interestingly, I don't see the "dispatch function" form documented in the dictionary.
What should "dispatch function" do?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Sat Jul 23, 2016 2:36 am

'dispatch function' calls the supplied function with the supplied arguments
it returns the result in the result (surprise!)
and returns 'handled' or 'unhandled' in 'it'.

Code: Select all

dispatch function "grunt" with "1234"

function grunt pValue
   return pValue * 2
end grunt
returns 2468 in the result.

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

Re: How to execute the result of a function when...

Post by FourthWorld » Sat Jul 23, 2016 2:47 am

Thanks. Definitely should be documented. When was that introduced? Was it mentioned in Release Notes?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Sat Jul 23, 2016 3:01 am

I'm not sure. Probably when the dispatch command was first introduced.
I'm as surprised as anyone else that it isn't documented.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to execute the result of a function when...

Post by Thierry » Mon Jul 25, 2016 9:34 am

mwieder wrote:Thierry-

Yes, it does/should work.

I also used the "send" form rather than "dispatch" because I can't remember how asynchronous the dispatch command is, and it might be important to end the mouseUp handler before processing the result of figureThis. For a single-line call like this it shouldn't matter, but if the executing command is blocking or otherwise takes a long time, the end result could be ambiguous.
Hi Mark,

I've been using this form of dispatch successfully for a long time.

Basically, I'm using this to code some FSM.

Here I'm talking of managing/transforming/parsing big data, and functions which are called millions of time;
so hunting every ms was a must. And this way of coding was the fastest.
Concerning the synchronus/asynchronous question, well, it really depends of the context, which
wasn't specified by the OP....

However, I did worked with LC 6.x for these projects
and I don't know if this behaves that way with LC 8.x versions.

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: How to execute the result of a function when...

Post by mwieder » Mon Jul 25, 2016 5:01 pm

Thierry-

Good to know. And as far as I know LC8 should act exactly the same as LC6 with regard to the dispatch/send commands.

Post Reply