I've got a bit of a problem with the `->() lfun. To illustrate, consider the following class:
---8<-- class Base { mixed `->(string x) { return (x == "add_one" && lambda(int x) { return x+1; }) || (x == "add_two" && lambda(int x) { return x+2; }); } } ---8<---
I now have a class which allows me to do Base()->add_one(7) to get 8, and Base()->add_two(7) to get 9. So far so good.
Now, let's try to specialize this class:
---8<--- class Sub { inherit Base; string add_two(int x) { return x+"two"; } } ---8<---
FAIL! Sub()->add_two(7) still returns 9. And what's more, even if I try to add something with a name that doesn't exist in Base, I still can't access it outside of Sub.
Is there a way around this? I tried using `::->() as a fallback in `->(), but it didn't have the desired effect.
FAIL! Sub()->add_two(7) still returns 9. And what's more, even if I try to add something with a name that doesn't exist in Base, I still can't access it outside of Sub.
Is there a way around this? I tried using `::->() as a fallback in `->(), but it didn't have the desired effect.
You probably want to use ::`->() with a second argument of 2 (cf object.c:f_magic_index() and preceeding comments).
Marvelous! (Why does zero_type 2 come into mind... :)
Maybe you also have a solution to the next problem:
How do I get
string add_two(int x) { return ::add_two(x)+"two"; }
to work in Sub?
How do I get
string add_two(int x) { return ::add_two(x)+"two"; }
to work in Sub?
The easiest way is probably to use the getter/setter syntax instead:
class Base { mixed `add_one() { return lambda(int x) { return x+1; }; }
mixed `add_two() { return lambda(int x) { return x+2; }; } }
class Sub { inherit Base; string add_two(int x) { return ::add_two(x)+"two"; } }
The obvious alternative is something like:
string add_two(int x) { return ::`->("add_two")(x)+"two"; }
Neither of these are working solutions. The first because the names "add_one" and "add_two" are not statically known in the real case, but dynamic data from the parent object. The second becase, come on, you can't really expect anyone who wants to override a method in a class to write something corny like that? I can write convoluted stuff in Base, because it's just one class, but Sub must be convenient to write.
pike-devel@lists.lysator.liu.se