by pphysch · 3y ago · 0 comments
In 2023, tooling is at the point where RTFM is secondary for mainstream lang libraries (TFM being embedded into the editor in intelligent ways). I will not go back.
It is not just about getting a function from an object and then executing that function.
It is executing that function in the context of the object from which you get it, by utilizing the pseudo-variable "this" (or 'self' in Smalltalk) which refers to that object from which you got that function (a.k.a "method" in this case).
But definitely the ability to have editor-support for lookup of argument-types etc. is a great benefit too.
Ignoring runtime polymorphism for the moment, there's nothing special about "this" or "self". It's just another function parameter. Consider how in Python it is actually explicitly declared as a parameter in the method declaration.
It is special in that when a method executes say in JavaScript, the 'this' has a very specific value even though you did not pass in an argument of that name nor did you ever assign a value to a local variable of that name. Depending on the language you use trying to assign to the (pseudo-) variable 'this' may or may not cause an error.
The "automatic" value 'this' has makes it special, different from other variables and arguments. That automatic value ties the method-call-syntax into the semantics of what it means to call a method as opposed to calling a free function.
This can be exemplified in JavaScript easily:
let funk = myOb.funk;
let v = myOb.funk();
let v2 = funk(); // throws error
In this case the error gets thrown if
myOb.funk internally refers to 'this'
and tries to access some field of it.
That causes an error because 'this' is
undefined inside 'funk' when it is called
as a plain function.When you call myOb.funk() there is NO error in the same case, because 'this' is then NOT undefined, its value is 'myOb'.
You can access any field of myOb inside the code of myOb.funk when you call it as myOb.funk().
That is a big semantic difference, not just "syntactic sugar".
It's just an implicit parameter named `this` whose argument happens to appear to the left of the `.` instead of after the `(`. Yes, there's a little extra work in the language to support this, but it's not particularly complex or deep semantically.
(In fact `this` is particularly semantically shallow in JavaScript because as your example notes, it's not even bound when a reference to a method is taken. In most other languages, `myOb.funk` will give you a function that partially applies `this`.)
Also lots of languages have syntactic sugar around the corresponding object e.g. implicitly dereference it for attribute access and method calls, or even give it exclusive(ish) properties like @ in Ruby (or straight up instance variable access in smalltalk).
Even Python treats it specially, given a method `foo`, `obj.foo` actually returns a proxy object which partially applies `foo` to `obj`. This only works on functions defined on the class object, mere callables set on the instance don't get that treatment.
The parent comment specifically said "by utilizing the pseudo-variable 'this' (or 'self' in Smalltalk)", so I was referring to that. Yes, in languages where the receiver is implicitly added to the lexical scope chain, things get a bit more complex.
> or even give it exclusive(ish) properties like @ in Ruby (or straight up instance variable access in smalltalk). Even Python treats it specially, given a method `foo`, `obj.foo` actually returns a proxy object which partially applies `foo` to `obj`. This only works on functions defined on the class object, mere callables set on the instance don't get that treatment.
Sure, there's other features that object-oriented languages tend to hang off methods too, but my point was just that from the perspective of within a method body, the receiver's mostly just another parameter. This is made explicit in some languages:
No, method syntax is just syntax. In some languages methods are more than just syntax sugar, but there's nothing special about the syntax itself.