accessor-functions would be nice, but they would incur significant overhead. In particular, looking up the `-> and `->= functions can be done in O(1) time, but looking up a function with a varying name currently takes O(log(n)) time (where n is the number of functions in the class) In a language with static binding, this is of course not a problem. Unfortunately pike only has static binding within objects, as soon as you call another object, dynamic binding is used.
A workaround for this problem would be to convert the method lookup function to use a hash-table instead of a binary seek. That way, method lookups would *always* be O(1), and while accessor functions would still incur some overhead, it wouln't be nearly as bad. (Also, converting the lookup function would make it possible to get rid of a function lookup cache, which could be a very nice net gain.)
The drawback is that hash tables use a bit more memory than binary seeks, but I don't think that's a major issue in this day and age. Especially since memory usage is linear to the total number of functions in the program, not the number of instances.
/ Fredrik (Naranek) Hubinette (Real Build Master)
Previous text:
2003-12-21 14:16: Subject: C#-like properties
The way to do that in Pike currently is to overload `-> and `->=, but that has several disadvantages when it comes to inheritance. If it's possible to overload indexing for specific identifiers, it'd become much less of a problem. It could look like this:
int `->foo() {return foo;} void `->foo= (int value) {foo = value;}
Even if it doesn't solve the inheritance problem, this would be a nice addition imho. It's easier to write than full-blown `-> and `->=, if nothing else.
Footnote: I think it has been mentioned before, but the inheritance problem is this:
class Foo { // This class emulates an identifier foo private int value; mixed `-> (string index) { if (index == "foo") return value; else return ::`-> (index); } void `->= (string index, mixed val) { if (index == "foo") value = val; else ::`->= (index, val); } }
class Bar { inherit Foo; void f() { foo = 17; // Error - the inherited identifier is not really there. } }
The identifier specific `->foo and `->foo= could solve that too, but then they're not really overload functions for -> and ->= anymore.
/ Martin Stjernholm, Roxen IS