Callable object
This article needs more citations. (May 2017) |
A callable object, in computer programming, is any object that can be called like a function.
In different languages
[edit]In C++
[edit]- 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.
In C++, any class that overloads the function call operator operator() may be called using function-call syntax.
import std;
struct Foo {
void operator()() const {
std::println("Called.");
}
};
int main() {
Foo instance;
instance(); // This will output "Called." to the screen.
}
In C#
[edit]In PHP
[edit]PHP 5.3+ has first-class functions that can be used e.g. as parameter to the usort() function:
$a = array(3, 1, 4);
usort($a, function ($x, $y) { return $x - $y; });
It is also possible in PHP 5.3+ to make objects invokable by adding a magic __invoke() method to their class:[1]
class Minus
{
public function __invoke($x, $y) { return $x - $y; }
}
$a = array(3, 1, 4);
usort($a, new Minus());
In Python
[edit]In Python, any object with a __call__() method (as well as all functions, classes, and methods) can be called using function-call syntax. The built-in callable function can approximately detect whether a given object is callable or not.[2][3]
class Foo:
def __call__(self) -> None:
print("Called.")
class Bar:
...
instance: Foo = Foo()
instance() # Outputs "Called." to the screen
assert(callable(instance)) # True since instance has __call__
assert(callable(zip)) # True since zip is a function, despite not having __call__
assert(callable(Bar)) # True since Bar is a class, despite not having __call__
assert(callable(lambda x: x)) # True since lambdas have __call__
A collections.abc.Callable type annotation can be used to declare that a given variable must be callable. A Callable[P, R], where P is a list of types and R is a type, can be called with values matching P as arguments and returns a value of type R. Other values for P, such as a ParamSpec or ..., denote more flexible cases.[4]
def spam(ham: int, eggs: str) -> None:
return None
func: Callable[[int, str], None] = spam
none: None = func(0, "foo")
In Dart
[edit]
Callable objects are defined in Dart using the call() method.
class WannabeFunction {
call(String a, String b, String c) => '$a $b $c!';
}
main() {
var wf = new WannabeFunction();
var out = wf("Hi","there,","gang");
print('$out');
}
In Swift
[edit]In Swift, callable objects are defined using callAsFunction.[6]
struct CallableStruct {
var value: Int
func callAsFunction(_ number: Int, scale: Int) {
print(scale * (number + value))
}
}
let callable = CallableStruct(value: 100)
callable(4, scale: 2)
callable.callAsFunction(4, scale: 2)
// Both function calls print 208.
References
[edit]- ↑ PHP Documentation on Magic Methods
- ↑ "Glossary § callable". Python 3.14.7 documentation. Python Software Foundation. 2026-09-01. Retrieved 2026-09-01.
{{cite web}}: CS1 maint: url-status (link) - ↑ "Built-in Functions § callable". Python 3.14.7 documentation. Python Software Foundation. 2026-09-01. Retrieved 2026-09-01.
{{cite web}}: CS1 maint: url-status (link) - ↑ "typing — Support for type hints § Annotating callable objects". Python 3.14.7 documentation. Python Software Foundation. 2026-09-01. Retrieved 2026-09-01.
- ↑ "A Tour of the Dart Language". www.dartlang.org. Retrieved 2019-03-25.
- ↑ "Declarations — The Swift Programming Language (Swift 5.6)". docs.swift.org. Retrieved 2022-02-28.
External links
[edit]