Page 1 of 1

Handler Parameters?

Posted: Sat Mar 14, 2015 12:09 am
by netdzynr
Can a handler accept multiple values?

It seems like it's only possible pass one value to a handler, or otherwise you need to declare your parameters/arguments as separate variables. Thanks for clarifying here.

Re: Handler Parameters?

Posted: Sat Mar 14, 2015 2:42 am
by WaltBrown
<Sorry, I didn't see the mailbox this was in...>

Re: Handler Parameters?

Posted: Sat Mar 14, 2015 8:02 am
by LCMark
@netdznr: Can you clarify what you mean (in case you have encountered a bug)?

Handlers in LCB are a little different from LiveCode Script.

First of all, there is no distinction between commands and functions - there are just handlers. Handlers which don't return a value simply set the 'the result' to 'undefined' in the caller after execution. You can call handlers with as a command, or as a function:

Code: Select all

handler myHandler()
  get myHandler()
end handler
You can specify parameters in the same was as you can in LCS, but you can type the parameters, and also specify a mode:

Code: Select all

handler myHandler(in pInParam as Integer, out rOutParam as String, inout xInOutParam)
end handler
An 'in' parameter is the same as in LCS, the value from the caller is copied to the parameter variable when the handler is called.

An 'out' parameter doesn't exist in LCS. Instead, you are expected to put a value into an out parameter's variable during execution of the handler. When the handler finishes executing, this value is copied into the variable (or container) you passed to it.

An 'inout' parameter is a combination of both - the value is copied from the caller at the start of the handler, and the value in the parameter variable is copied back to the caller when the handler finishes.

One thing where LCB is a little more powerful than LCS at the moment, is that you can actually pass chunk expressions to out and inout variables:

Code: Select all

handler callMyHandler()
  variable tString
  put "Hello World" into tString
  myHandler(tString, char 1 to 5 of tString, char 6 to -1 of tString)
end handler
In this case, in myHandler, the pInParam variable will have value "Hello World", the rOutParam will have no value at the start, but will replace char 1 to 5 of tString with the value you place in it within the handler, and xInOutParam will have value " World" on entry, and whatever you change that to in the handler will be placed in char 6 to -1 of tString when the handler finishes.

So, yes, handlers (just as in LCS) can have as many parameters as you like - but you also have more options as to how parameters are passed.