const challenge

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 31 19:15:36 PST 2008


"Janice Caron" wrote
> OK, here's an example, borrowed a bit from Tango.
>
>    char[] niftyStringFunction(char[] outBuffer, char[] input)
>    {
>        /*...*/
>    }
>
> The idea is, if (outBuffer == null) then the function allocates memory
> and returns it; whereas, if (outBuffer != null) then the result is
> writen into outBuffer, and the return value is a slice of outBuffer.
> (The idea is to minimize allocations).
>
> Converting this to D2 is problematic, because you want to return a
> string, but string is invariant, so if you do that, you won't be able
> to pass the same buffer back into other, similarly nifty string
> functions.
>

char[] niftyStringFunction(char[] outBuffer, const(char)[] input)

The function contract says, "I may write to outBuffer, I won't write to 
input.  You can write to the result if you wish".

How is this not what you want?

-Steve 





More information about the Digitalmars-d mailing list