See also: Function object.
A callable object, in computer programming, is any object that can be called like a function.
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.
struct Foo
int main
PHP 5.3+ has first-class functions that can be used e.g. as parameter to the usort
function:
It is also possible in PHP 5.3+ to make objects invokable by adding a magic __invoke
method to their class:[1]
$a = array(3, 1, 4);usort($a, new Minus);
In Python any object with a __call__
method can be called using function-call syntax.
foo_instance = Foofoo_instance # This will output "Called." to the screen.[2]
Another example:
def __call__(self, x): self.n += x return self.n
Callable objects are defined in Dart using the call
method.
main [3]
In Swift, callable objects are defined using callAsFunction
.[4]