'new' class method

dsimcha dsimcha at yahoo.com
Thu Oct 23 13:55:01 PDT 2008


== Quote from Bill Baxter (wbaxter at gmail.com)'s article
> It kills static opCall for classes, but who cares.

Actually, I sometimes use static opCall to encapsulate the creation of temp
variables that I might want to recycle for performance reasons in some cases,
though more likely with a struct than with a class.  Example:

SomeType foo() {
    auto temp = new SomeType[someNumber];
    doStuff;
    return someStuff;
}

would become:

struct foo {
    SomeType[] temp;

    static opCall() {
        //Creates a foo, simulates a regular function call.
        //Good for when I don't care about recycling temp storage.
        foo f;
        return f.doStuff;
    }

    SomeType doStuff() {
        temp.length = someNumber;  //Only allocates first time.
        doStuff;
        return temp;
    }
}

This way, I can either use it the clean, easy way via the static opCall simulating
a regular function call, or the efficient way by making an explicit instance of
the struct, and doStuff() in a loop.



More information about the Digitalmars-d mailing list