You're viewing a comment by Jan Seidl and its responses.

May 17, 2010, 13:59

Very nice article! It's sad that other popular languages doesn't implement operator()-like

Keep up good work Pete!

Comment Responses

assd Permalink
August 06, 2011, 16:21

why? I think it's good thing, using () only produces mess in code. When I see bomb.explode() I now I should expect the object bomb to explode. But bomb() doesn't say me anything. Worst thing, the requirements for () changes, old comment stays and i can find something like:
// this operator explodes the bomb
operator()() { /* explode() */ save_the_world(); }

AP2 Permalink
August 06, 2011, 23:24

Python does - you just need to implement the __call__ method:

class MyClass:
    def __call__(self):
        print "hello"

obj = MyClass()
obj() #prints "hello"
August 08, 2011, 15:05

All functions in Javascript are implicitly C++-functors, but defined in a reverse order.

function foo() {;
} = 0;

Each run will increment the value and print the new version, but you can also alter that property however and whenever you like, so in Javascript you tend to use real closures for holding state, instead:

var foo = (function() {
    var abc = 0;
    return function() {

or you can define objects to instantiate if you need more than one:

function Foo() {
    var abc = 0;
    return function() {
var foo = new Foo();
robinst Permalink
August 09, 2011, 22:10

Scala also has "()":

class Foo {
  def apply(name: String) {
    println("Hello " + name)

val foo = new Foo
foo("Jan")  // prints Hello Jan
February 26, 2016, 12:45

In Pascal you can at least override the []-operator for classes

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "computer_219": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.