Специализни шаблонца! (udpn) wrote,
Специализни шаблонца!
udpn

Lambda methods

There's an annoying problem in PHP, JS and other languages with OOP and lambda functions: this keyword in a lambda defined in some method. Lambda function's scope differs from method's scope: there's just no this. Even worse, methods and fields may be declared private in PHP, so if we somehow pass this into lambda function, we still won't be able to access those fields and methods. I suppose the most significant reason for this is that methods are not just functions, and using function keyword for them is a misnomer. Methods are functions with one more implicit parameter named this (in C++ world it's called "thiscall" calling convention).

So why don't we call them method etc.? Then if we saw
class Test {
    private $x;
    method f() {
        return g(method() {
            return $this->x;
        });
    }
}

we would be sure that lambda method took the same this as its enclosing method, and the problem would disappear. In case of Javascript, library authors (e.g. Google) wouldn't have to create optional arguments to take something that would be substituted as this for callback function.

There's something wrong with our industry if we don't see it.
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments