True, but that doesn't solve quite the same problem since it wouldn't make it possible to implement string-like objects that can't store the string data that way. E.g. deferred locale strings need to get a function called to retrieve the right value. It'd be nice to have both variants.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-01-30 18:31: Subject: Re: OpenSSL wrapper vs Pike's SSL (Was: Bz2)
One interesting trick if one wants to objectify all values and at the same time have good performance, is to make some classes "top-wired". Every class can inherit at most one top-wired class, directly or indirectly.
For each instance, there's a field in the header that says which top-wired class (if any) that is inherited by the object's class. The data associated with that class is always stored first in the instance data area, at a fix offset.
Thus, if strings are top-wired, C code can operate on any subclass
if (o->top_wired == GENERIC_STRING_TYPE) { struct generic_string *s = o->data; ... } else error("Expected string"); /* Or generic code using method calls * instead of accessing the internal representation */
which shouldn't be slower than
if (o->type == PIKE_T_STRING) { ... }
/ Niels Möller ()