# Callable object

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Callable_object
> Markdown URL: https://mediated.wiki/source/Callable_object.md
> Source: https://en.wikipedia.org/wiki/Callable_object
> Source revision: 1308816110
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{See also|Function object}}
{{more citations needed|date=May 2017}}

A '''callable object''', in [computer programming](/source/computer_programming), is any object that can be called like a [function](/source/subroutine).

==In different languages ==

=== In C++ ===
* [pointer to function](/source/subroutine_pointer);
* pointer to [member function](/source/member_function);
* [functor](/source/function_object);
* [lambda expression](/source/Anonymous_function).
* <code>std::function</code> is a [template class](/source/template_class) that can hold any callable object that matches its signature.

In C++, any class that [overloads](/source/operator_overloading) the function call operator <code>operator()</code> may be called using function-call syntax.
<syntaxhighlight lang="cpp">
import std;

struct Foo {
    void operator()() const {
        std::println("Called.");
    }
};

int main() {
   Foo instance;
   instance(); // This will output "Called." to the screen.
}
</syntaxhighlight>

=== In C# ===
* [delegate](/source/delegate_(CLI));
* [lambda expression](/source/Anonymous_function).

=== In PHP ===

[PHP](/source/PHP) 5.3+ has [first-class function](/source/first-class_function)s that can be used e.g. as parameter to the <code>usort()</code> function:

<syntaxhighlight lang="php">
$a = array(3, 1, 4);
usort($a, function ($x, $y) { return $x - $y; });
</syntaxhighlight>

It is also possible in PHP 5.3+ to make objects invokable by adding a magic <code>__invoke()</code> method to their class:<ref name="phpinvoke">[http://php.net/manual/en/language.oop5.magic.php#object.invoke PHP Documentation on Magic Methods]</ref>

<syntaxhighlight lang="php">
class Minus
{
    public function __invoke($x, $y) { return $x - $y; }
}

$a = array(3, 1, 4);
usort($a, new Minus());
</syntaxhighlight>

=== In Python ===
In [Python](/source/Python_(programming_language)) any object with a <code>__call__()</code> method can be called using function-call syntax.

<syntaxhighlight lang="python">
class Foo:
    def __call__(self) -> None:
        print("Called.")

instance: Foo = Foo()
instance()  # This will output "Called." to the screen.
</syntaxhighlight><ref>{{cite web|last1=Bösch|first1=Florian|title=What is a "callable" in Python?|url=https://stackoverflow.com/a/111255/5513567|website=StackOverflow.com|access-date=24 September 2017}}</ref>

Another example:
<syntaxhighlight lang="python">
class Accumulator:
    def __init__(self, n: int) -> None:
        self.n = n

    def __call__(self, x: int) -> int:
        self.n += x
        return self.n
</syntaxhighlight>

=== In Dart ===
Callable objects are defined in [Dart](/source/Dart_(programming_language)) using the <code>call()</code> method.<syntaxhighlight lang="dart">
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');
}
</syntaxhighlight><ref>{{Cite web|url=https://www.dartlang.org/guides/language/language-tour|title=A Tour of the Dart Language|website=www.dartlang.org|access-date=2019-03-25}}</ref>

=== In Swift ===
In [Swift](/source/Swift_(programming_language)), callable objects are defined using <code>callAsFunction</code>.<ref>{{Cite web |title=Declarations — The Swift Programming Language (Swift 5.6) |url=https://docs.swift.org/swift-book/ReferenceManual/Declarations.html |access-date=2022-02-28 |website=docs.swift.org}}</ref>
<syntaxhighlight lang="swift">
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.
</syntaxhighlight>

==References==
{{Reflist}}

== External links ==
* [https://web.archive.org/web/20151115042817/http://en.cppreference.com/w/cpp/concept/Callable C++ Callable concept]

Category:Articles with example C++ code
Category:Articles with example C Sharp code
Category:Articles with example PHP code
Category:Articles with example Python (programming language) code
Category:Articles with example Swift code
Category:Object (computer science)
Category:Subroutines

{{compu-prog-stub}}

---
Adapted from the Wikipedia article [Callable object](https://en.wikipedia.org/wiki/Callable_object) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Callable_object?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
