News for hackers. Stuff that matters.
Re:Crafting Interpreters: Superclasses by wool_gather on Wednesday March 18, 2020 @01:10PM (#22618858)
Interesting point. I'd have to look at my implementation to be sure, but Lox will also have this quirk, won't it? You can freely assign values, (as I recall) including functions, to a property on an instance.

Or does it only come up when a method is added to the class?

Re:Crafting Interpreters: Superclasses by munificent on Wednesday March 18, 2020 @01:44PM (#22619257)
Lox makes a distinction between functions and methods, so you won't run into this problem. This is unlike JavaScript, but like most other object-oriented languages.

If you take a function (which may be a reference to a method) and store it in a field on some instance, the function remembers its original bindings to "this" and "super", which is generally what you want and expect.

Lox doesn't allow you to mutate a class by adding new methods to it.

Re:Crafting Interpreters: Superclasses by pansa2 on Wednesday March 18, 2020 @01:18PM (#22618954)
> Lox will also have this quirk, won't it?

Possibly - I’m not sure about the details of method lookup in Lox. Does `this` even work correctly when a method is assigned to an instance?

Re:Crafting Interpreters: Superclasses by munificent on Wednesday March 18, 2020 @01:45PM (#22619270)
If you store a function or reference to a method in a field on an instance in Lox, the function will remember its original `this` binding. That's what Python, C#, Ruby, etc. do and is generally what you want. It's not what JS does because JS doesn't really know what a "method" is, or at least didn't before classes were added.
Re:Crafting Interpreters: Superclasses by pansa2 on Wednesday March 18, 2020 @01:52PM (#22619357)
Is it possible that there isn't a `this` binding? Does Lox allow access to "unbound" methods directly from a class, or are methods accessible only via instances?
Re:Crafting Interpreters: Superclasses by munificent on Wednesday March 18, 2020 @02:07PM (#22619536)
Methods are only accessible from instances, so `this` is always meaningfully bound. If you try to use `this` outside of a class body, it's a static error.
Re:Crafting Interpreters: Superclasses by pansa2 on Wednesday March 18, 2020 @02:23PM (#22619691)
OK, that solves the problem. The issue with unbound methods in Python is that they don’t yet have a value for `this`, but they do have a fixed value for `super`.