Page 1 of 2

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

Posted: Fri Jul 22, 2016 1:00 am
by melristau
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

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

Posted: Fri Jul 22, 2016 2:12 am
by FourthWorld
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?

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

Posted: Fri Jul 22, 2016 6:14 am
by dunbarx
Richard.

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

Craig Newman

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

Posted: Fri Jul 22, 2016 7:49 am
by Thierry
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

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

Posted: Fri Jul 22, 2016 7:23 pm
by mwieder
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

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

Posted: Fri Jul 22, 2016 8:10 pm
by Thierry
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

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

Posted: Fri Jul 22, 2016 8:30 pm
by mwieder
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.

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

Posted: Fri Jul 22, 2016 8:43 pm
by [-hh]
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 ...

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

Posted: Fri Jul 22, 2016 9:41 pm
by mwieder
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.

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

Posted: Sat Jul 23, 2016 2:14 am
by FourthWorld
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?

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

Posted: Sat Jul 23, 2016 2:36 am
by mwieder
'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.

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

Posted: Sat Jul 23, 2016 2:47 am
by FourthWorld
Thanks. Definitely should be documented. When was that introduced? Was it mentioned in Release Notes?

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

Posted: Sat Jul 23, 2016 3:01 am
by mwieder
I'm not sure. Probably when the dispatch command was first introduced.
I'm as surprised as anyone else that it isn't documented.

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

Posted: Mon Jul 25, 2016 9:34 am
by Thierry
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

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

Posted: Mon Jul 25, 2016 5:01 pm
by mwieder
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.