A callable object, in
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, is any object that can be called like a
function
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
.
In different languages
In C++
*
pointer to function;
* pointer to
member function
A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
;
*
functor
In mathematics, specifically category theory, a functor is a mapping between categories. Functors were first considered in algebraic topology, where algebraic objects (such as the fundamental group) are associated to topological spaces, and m ...
;
*
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
Delegate or delegates may refer to:
* Delegate, New South Wales, a town in Australia
* Delegate (CLI), a computer programming technique
* Delegate (American politics), a representative in any of various political organizations
* Delegate (Unit ...
;
*
lambda expression.
In PHP
PHP
PHP is a general-purpose scripting language geared toward 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 functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from ...
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
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
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
* SWIFT, ...
, 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
{{comp-stub