string is rarely useful as a function argument

Jonathan M Davis jmdavisProg at gmx.com
Thu Dec 29 14:12:46 PST 2011


On Thursday, December 29, 2011 17:01:19 deadalnix wrote:
> Le 28/12/2011 21:43, Jonathan M Davis a écrit :
> > Agreed. And for a number of functions, taking const(char)[] would be
> > worse, because they would have to dup or idup the string, whereas with
> > immutable(char)[], they can safely slice it without worrying about its
> > value changing.
> 
> Is inout a solution for the standard lib here ?
> 
> The user could idup if a string is needed from a const/mutable char[]

In some places, yes. Phobos doesn't use inout as much as it probably should, 
simply because it was only recently that inout was made to work properly. 
Regardless, you have to be careful about taking const(char)[], because there's 
a risk of forcing what could be an unnecessary idup. The best solution to 
that, however, depends on what exactly the function is doing. If it's simply 
slicing a portion of the string that's passed in and returning it, then inout 
is a great solution. On the other hand, if it actually needs an 
immutable(char)[] internally, then there's a good chance that it should just 
take a string. It depends on what the function is ultimately doing.

- Jonathan M Davis


More information about the Digitalmars-d mailing list