Callback (computer Programming) - Implementation

Implementation

The form of a callback varies among programming languages.

  • C, C++ and Pascal allow function pointers as arguments to other functions. Other languages, such as JavaScript, Lua, Python, Perl and PHP, simply allow the name of a function or a function literal (lambda expression) to be passed through.
  • CLI languages such as C# and VB.NET, provides a type-safe encapsulating reference, a 'delegate', to define well-typed function pointers. These can be used as callbacks.
  • Events and event handlers, as used in .NET languages, provide generalized syntax for callbacks.
  • Functional languages generally support first-class functions, which can be passed as callbacks to other functions, stored as data or returned from functions.
  • Some languages, such as Algol 68, Perl, Smalltalk, newer versions of C# and VB.NET as well as most functional languages, allow unnamed blocks of code (lambda expressions) to be supplied instead of references to functions defined elsewhere.
  • In some languages, e.g. Scheme, ML, JavaScript, Perl, Smalltalk, PHP (since 5.3.0), and many others, such functions can be closures, i.e. they can access and modify variables locally defined in the context in which the function was defined.
  • In object-oriented programming languages without function-valued arguments, such as Java, can be simulated by passing an instance of an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy.
  • C++ allows objects to provide their own implementation of the function call operation. The Standard Template Library accepts these objects (called functors), as well as function pointers, as parameters to various polymorphic algorithms


Read more about this topic:  Callback (computer Programming)