'new' class method

Bill Baxter wbaxter at gmail.com
Thu Oct 23 14:02:12 PDT 2008


On Fri, Oct 24, 2008 at 5:55 AM, dsimcha <dsimcha at yahoo.com> wrote:
> == 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.

There are definitely uses for static opCall, but it's all just for
cuteness' sake.  There's no reason you can't use a regular static
method instead.  Like

        temp.length = someNumber.tmp;  // static method call, only
allocates first time

--bb



More information about the Digitalmars-d mailing list