Callable object
A callable object, in computer programming, is any object that can be called like a function.
In different languages
In C++
- pointer to function;
- pointer to member function;
- functor;
- lambda expression.
std::functionis a template class that can hold any callable object that matches its signature.
operator may be called using function-call syntax.import std;
struct Foo ;
int main
In C#
In PHP
PHP 5.3+ has first-class functions that can be used e.g. as parameter to theusort 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.class Foo:
def __call__ -> None:
instance: Foo = Foo
instance # This will output "Called." to the screen.
Another example:
class Accumulator:
def __init__ -> None:
self.n = n
def __call__ -> int:
self.n += x
return self.n
In Dart
Callable objects are defined in Dart using thecall method.class WannabeFunction
main
In Swift
In Swift, callable objects are defined usingcallAsFunction.struct CallableStruct
let callable = CallableStruct
callable
callable.callAsFunction
// Both function calls print 208.