Nasal Callbacks Explained: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
mNo edit summary
m (Use Nasal highlighter)
 
Line 4: Line 4:


Consider the following snippet of code, which assigns a function to the say_hello symbol:
Consider the following snippet of code, which assigns a function to the say_hello symbol:
<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">


var say_hello = func() {
var say_hello = func() {
Line 17: Line 17:
But you could just as well call an anonymous function directly, without binding it to any symbolic name:
But you could just as well call an anonymous function directly, without binding it to any symbolic name:


<syntaxhighlight lang="php">
<syntaxhighlight lang="nasal">


(func() {
(func() {

Latest revision as of 21:08, 10 November 2013


Many Nasal functions accept callbacks as function arguments, i.e. you can basically pass around functions as arguments (and return values). Some of the more common uses in FlightGear are callbacks invoked by timers or by listeners. So a callback parameter is basically a "pointer" (reference) to a function. The function can be free-standing or part of some other data structure (vector/hash), or a method call, i.e. bound to a symbolic name - or an anonymous function, that is directly specified "inline", without any name:

Consider the following snippet of code, which assigns a function to the say_hello symbol:

var say_hello = func() {
 print("Hello!");
}

# to call the function:
 say_hello();


But you could just as well call an anonymous function directly, without binding it to any symbolic name:

(func() {
 print("Hello!");
}) ();

TODO, explain:

  • free functions with/without parameters
  • method calls with/without parameters
  • me calls with/without parameters
  • call()