develooper Front page | perl.perl6.internals | Postings from February 2001

Re: PDD 2, vtables

Simon Cozens
February 17, 2001 08:56
Re: PDD 2, vtables
Message ID:
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;
    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

    PMC mypmc;
    mypmc = (; /* Old value of mypmc is ignored */

Unless it's a pointer to a pointer to a structure. Which is possible, but

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 = (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. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About