Do first-class methods exist?

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"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật