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.

import std;

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

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

In C#

In PHP

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

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

class Foo:
    def __call__(self) -> None:
        print("Called.")

instance: Foo = Foo()
instance()  # This will output "Called." to the screen.

[2]

Another example:

class Accumulator:
    def __init__(self, n: int) -> None:
        self.n = n

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

In Dart

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');
}

[3]

In Swift

In Swift, callable objects are defined using callAsFunction.[4]

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

  1. ^ PHP Documentation on Magic Methods
  2. ^ Bösch, Florian. "What is a "callable" in Python?". StackOverflow.com. Retrieved 24 September 2017.
  3. ^ "A Tour of the Dart Language". www.dartlang.org. Retrieved 2019-03-25.
  4. ^ "Declarations — The Swift Programming Language (Swift 5.6)". docs.swift.org. Retrieved 2022-02-28.


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.