On Mon, Feb 05, 2001 at 05:14:44PM -0500, Dan Sugalski wrote:
> =item new
>
> void new(PMC[, key]);
>
> Creates a new variable of the appropriate type out of the passed PMC,
> destroying the current contents if there are any. This is a class
> function.
Can I suggest this becomes
PMC new(PMC[, key]);
It gets really hard to get things started otherwise:
PMC mypmc;
(sviv_vtable.new)(mypmc);
printf("mypmc's class is %s\n", (mypmc->vtbl->name)(mypmc));
If PMC is a pointer to a structure, "new" will need to allocate memory for a
new structure, and hence the value of mypmc will have to change. So I'd rather
say
PMC mypmc;
mypmc = (sviv_vtable.new)(mypmc); /* Old value of mypmc is ignored */
Unless it's a pointer to a pointer to a structure. Which is possible, but
yuck.
The structure
struct _pmc {
VTABLE* vtbl;
void* private_data;
};
makes it nice and easy to morph one PMC class into another; the "new" entry in
the hypothetical sviv_vtable becomes:
PMC int_new(PMC pmc, ...) {
if (pmc)
(pmc->vtbl->destroy)(pmc);
else
pmc = (PMC)malloc(sizeof(PMC));
pmc->vtbl = &sviv_vtable;
pmc->private_data = (IV*)malloc(sizeof(IV*));
return pmc;
}
--
If you give a man a fire, he'll be warm for a day. If you set a man on fire,
he'll be warm for the rest of his life.