Dan Sugalski <[EMAIL PROTECTED]> wrote:
At 5:04 PM -0500 1/18/05, Sam Ruby wrote:
f = "Parrot".find print f("r")
Note that I referenced the method as an attribute, and then called it as a function.
Mmm, syntax! :) Luckily it makes no difference to us at the parrot level. What that should translate to is something like:
$P0 = find_method Parrot_string, "find" # Elided check for failed lookup and fallback to attribute fetch $P1 = make_bound_method(Parrot_string, $P0)
Not quite. It's just:
f = getattribute Parrot_string, "find"
nothing more. The C<get_attr_str> vtable has to do the right thing, i.e. if the attribute is a callable, it has to return a bound method object.
Exactly.
Furthermore, the function "remembers" what object it is bound to. This is accomplished by VTABLE_find_method creating a new PyBoundMeth PMC which contains two references, one to the object, and one to the method.
While a good idea, I think it's not the right way to handle this. Binding objects to methods to create invokable subs is going to be something we're going to need for a lot of the languages, so I think we'd be better served providing a general facility to do it rather than leaving it to each individual language designer to do it. Should save some work all around too.
Yeah. When this came up last, I've proposed two ways to handle it:
1) inside the Sub/NCI PMC 2) by a distinct Bound_Meth PMC class derived from 1)
The latter is probably cleaner. Binding the object to the callable could be done e.g. by the C<set_pmc> vtable.
That's exactly how PyBoundMeth works today.
C<set_pointer> sets the pointer to the actual subroutine. C<set_pmc> sets the pointer to the "bound" object.
- Sam Ruby