Callable object


A callable object, in computer programming, is any object that can be called like a function.

In different languages

In C++

In C++, any class that overloads the function call operator operator may be called using function-call syntax.

  1. include
struct Foo
int main

In C#

5.3+ has first-class functions that can be used e.g. as parameter to the usort function:

$a = array;
usort;

It is also possible in PHP 5.3+ to make objects invokable by adding a magic __invoke method to their class:

class Minus
$a = array;
usort);

In Python

In Python any object with a __call__ method can be called using function-call syntax.

  1. Python 3 Syntax
class Foo:
def __call__:
print
foo_instance = Foo
foo_instance # This will output "Called." to the screen.

Another example:

class Accumulator:
def __init__:
self.n = n
def __call__:
self.n += x
return self.n

In Dart

To allow your Dart class to be called like a function, implement the call method.
class WannabeFunction
main