A callable object, in
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, is any object that can be called like a
function.
In different languages
In C++
*
pointer to function;
* pointer to
member function;
*
functor
In mathematics, specifically category theory, a functor is a Map (mathematics), mapping between Category (mathematics), categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) ar ...
;
*
lambda expression.
*
std::function
is a
template class that can hold any callable object that matches its signature.
In C++, any class that
overloads the function call operator
operator()
may be called using function-call syntax.
#include
struct Foo
;
int main()
In C#
*
delegate;
*
lambda expression.
In PHP
PHP
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
5.3+ has
first-class function
In computer science, a programming language is said to have first-class functions if it treats function (programming), functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning ...
s that can be used e.g. as parameter to the
usort()
function:
$a = array(3, 1, 4);
usort($a, function ($x, $y) );
It is also possible in PHP 5.3+ to make objects invokable by adding a magic
__invoke()
method to their class:
[PHP Documentation on Magic Methods](_blank)
/ref>
class Minus
$a = array(3, 1, 4);
usort($a, new Minus());
In Python
In Python any object with a __call__()
method can be called using function-call syntax.
class Foo:
def __call__(self):
print("Called.")
foo_instance = Foo()
foo_instance() # This will output "Called." to the screen.
Another example:
class Accumulator:
def __init__(self, n):
self.n = n
def __call__(self, x):
self.n += x
return self.n
In Dart
Callable objects are defined in Dart using the call()
method.
class WannabeFunction
main()
In Swift
In Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIF ...
, callable objects are defined using callAsFunction
.
struct CallableStruct
let callable = CallableStruct(value: 100)
callable(4, scale: 2)
callable.callAsFunction(4, scale: 2)
// Both function calls print 208.
References
External links
C++ Callable concept
Articles with example C++ code
Articles with example C Sharp code
Articles with example PHP code
Articles with example Python (programming language) code
Articles with example Swift code
Object (computer science)
Subroutines
{{compu-prog-stub