Converting handlers to function values

In the previous example I used Function Values but I didn’t mention how they were created.
LingoF allows to define function values in several ways.
Maybe the most intuitive is to “convert” an existing function written as a handler to a function value.
We can do it using the fun shortcut.
We will see this shortcut frequently because it is overloaded, so depending on the type and number of parameters it does different tasks. But in fact it does always the same task: Create a function value.

To create a function value from an existing handler the syntax is:

fun(#{handler}, {script instance}, {number of expected parameters})

So in the previous example we could have created the add function this way:
( in a script named “my functions” )

on add me, x,y
  return x+y
end

then in the message window ( or anywhere in the code )

add = fun(#add,script "my functions",2)

Note that the first parameter me is ignored, this behavior is always so unless you pretend to use a global handler defined in a movie script. In that case you can specify _movie as instance script.

Leave a Reply

Security Code: