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?
1
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.)
3
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.
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!'};
1
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.
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"