Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Okay, I know first-class functions are cool, closures even better, etc. But is there any language with first-class methods?

In my mind, I see a first-class method as an "object" that has both a function pointer and a pointer to a specific instance of the class/object, but the implementation doesn't matter. I just want to know if there is any language that uses them.

And as a bonus, how were they implemented?

share|improve this question
1  
Do you mean with "first-class method" a method that gets bound to an object at point A in the code, passed around and finally invoked at point B? –  Bart van Ingen Schenau Aug 23 at 7:34

5 Answers 5

up vote 4 down vote accepted

In typical OO parlance, methods belong to objects. If methods were themselves objects, then these objects would of course have methods. But those are objects, too, so they have methods. Which are objects which have methods which are objects which have methods which are objects which have methods …

There are ways to deal with this sort of recursion, but the usual way to deal with this problem is to not have methods be first-class.

In Ruby, for example, there are reflective methods that give you proxy objects for methods (those proxy objects are of class Method), but those aren't the methods themselves, they are only proxies that represent the methods.

However, in Self, objects don't have methods, they only have slots (fields in Java-lingo, instance variables in Ruby). Sending a message to an object does not invoke a method, it activates a slot. Slots can be either passive or active. Passive slots simply return the object referenced by them, active slots run the object referenced by them. So, methods are actual first-class objects which simply get assigned to active slots. (If you have used ECMAScript, you might recognize its Self heritage here. ECMAScript doesn't distinguish between passive and active slots, though, instead it uses special syntax for executing the object returned by the property.)

share|improve this answer
    
I think that's right. First class means you can store it, compare it, retrieve it and slots do that. –  david.pfx Aug 23 at 8:52
    
Interesting; I didn't know that Self did that. (I don't know ECMAScript either, by the way.) You mentioned a recursion problem of methods having methods. I didn't think of that, but at the same time, is that really a problem? After all, you don't have to define methods on a method. –  gdhoward Aug 23 at 17:23
1  
@gdhoward: You only need either fields or methods anyway. If you have fields, then you don't need methods, because "methods" can be just some executable code stored in a field. (This is the approach taken by Self, ECMAScript or Python.) If you have methods, then you don't need fields, because "fields" can be just a pair of getter/setter methods. (This is the approach taken by Newspeak.) Most OO languages, however, have both, even though they don't actually need them. (And unfortunately, they don't treat them uniformly with the notable exception of Scala, which gets this right.) –  Jörg W Mittag Aug 23 at 17:29

In Python, methods are regular fields, but callable. Also they have a self parameter that is explicitly declared, but implicitly passed; though you can invoke any method on the class object, passing an instance explicitly.

class A:
    def __init__(self, val):
        self._val = val
    def fun(self):
        print(self._val)

def invoke(f):
   f()

def invoke_with_param(f, par):
    f(par)

x1 = A(1)
x2 = A(2)

invoke(x1.fun)
invoke(x1.fun)
A.fun(x1)
A.fun(x2)
invoke_with_param(A.fun, x1)
# etc.
share|improve this answer

Yes, in javascript. That's because in javascript methods are functions and functions are full-fledged objects.

Example:

// Here's a "class" with method foo.
// Note that javascript don't really have classes,
// we use constructors instead:
function FooTeNator () {
    this.text = "foo";
}
FooTeNator.prototype.foo = function () {return this.text}

Notice the first interesting thing: methods are merely first class functions assigned to our constructor's prototype.

Regular method call:

var foomatic = new FooTeNator();
foomatic.foo(); // returns "foo"

Redefining a new method after we've created the object:

var foomatic = new FooTeNator();
foomatic.foo = function () { return "" }
foomatic.foo(); // returns empty string

Letting another object borrow our method:

var foomatic = new FooTeNator();
var other_object = {}; // {} is a plain object in object literal syntax
other_object.text = "bar";
other_object.foo = foomatic.foo;
other_object.foo(); // returns "bar"

Letting another object borrow our method from our constructor:

var other_object = {text : "bar"};
other_object.foo = FooTeNator.prototype.foo;
other_object.foo(); // returns "bar"

Calling our method while temporarily re-assigning the value of this inside it to another object:

var foomatic = new FooTeNator();
var other_object = {text : "bar"};
foomatic.foo.call(other_object); // returns "bar"

I think that qualifies as first-class method. Of course, this is merely first-class function. But with regards to OO the function is not a regular function because we reference this in it.


Side Note:

Javascript has a strange concept of this. Even regular functions can reference this outside of objects. When not bound to any object javascript will bind this to the global scope or DOM object or undefined depending on circumstances.

Side, Side Note:

When I said functions are objects in javascript it really means what it means. Functions (hence methods) have a constructor, the Function function. Functions/methods can also themselves have methods:

// Creating a method using the Function constructor
var my_object = {
    strange_method : new Function('x','return x');
}
// Assigning a method to our method:
my_object.strange_method.sub_method = function () {return 'HA!'};
share|improve this answer
    
Trippy. I never knew Javascript could do that. It scares me now. +1 –  gdhoward Aug 23 at 17:22

C++11 lambdas:

struct Foo{
    int bar(){
        return 42;
    }
};

Foo f;
auto methodobject = [&]{ return f.bar(); };
int i = methodobject(); //calls f.bar

Lambdas are implemented as regular classes with the captured objects (in this case Foo &f) as members and an auto operator (){/*whatever is in the lambda declaration*/}. With a bit more typing you can create a regular class that does the capturing and calling and you could change the object and method to call at runtime.

share|improve this answer

Functions in JavaScript are first-class citizens (objects with methods). There are no methods per se, but every function can be invoked as a method on any object. Functions can also be assigned to fields and invoked in c++-like object-oriented syntax.

var func = function(arg) {
  this.field = arg;
}

func.apply(null, ["value0"]); // 'func' invoked as a free function
// window.field == "value0"

var obj = {};
func.apply(obj, ["value1"]); // 'func' invoked as a method of 'obj'
// obj.field == "value1"

obj.method = func;
obj.method("value2")
// obj.field == "value2"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.