Open masak opened 7 years ago
Actually, given that
x.y()
in 007 (unlike in Perl 6, JavaScript and Java, but like Python) is grammatically just x.y
with a ()
postfixed, I don't think there's a way we can avoid having bound methods. Not if we want to talk about self
inside of them anyway.
In #242 I'm currently lifting the Q
type hierarchy, and the above necessity is becoming blindingly obvious.
I think the nicest thing about this is that something like
mylist.map(someobject.method)
will just automatically fall out of the deal and Do The Right Thing.
I just realized that this could also completely replace ::
as a namespace separator.
What today is Q::Statement::If
could be changed to Q.Statement.If
.
How would one define this in-language? Well, I think it'd make sense to allow nested classes to have this behavior:
class Q {
class Statement extends Q {
class If extends Statement {
}
}
}
(Classes would be "static" by default, unlike in Java.)
But I realize that this would favor the original author a lot in favor of later authors, who can't necessarily "open up" a class and nest new things in it.
So maybe we should also allow this syntax:
class Q.Statement.If {
}
In other words, unbound methods are subs, whose first parameter
self
is the invocant.
Worth pointing out that this is not possible right now, since D.foo
would refer to a foo
method on the type object D
, which (unlike Python) does not have all the methods instances of D
has. At least as far as the latest word on the 007 MOP is concerned.
I do see the advantage of having a syntax for unbound methods, though. Maybe if we free up ::
from being a namespace separator (as above), we could have D::foo
mean "an unbound foo
method on an instance of D
".
Just dropping this here: something as rare as a regret about bound methods. (Edit: I would summarize this regret as "bound methods interact confusingly with method overloading".)
I've concluded that Python has this one exactly right.
In other words, unbound methods are subs, whose first parameter
self
is the invocant. Every time you access a method through an object, you get a bound method, which is simply a functional wrapper that looks like this:Unlike Python, I'm going to go with a method keyword instead of having to explicitly write
self
as a first parameter. But the result of the method keyword will be a regular function withself
thus injected:Later we might want to generalize this to Python-like descriptors. But one step at a time.