Why does toStringz return an immutable(char)*?

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 18 20:51:48 PDT 2011


On 2011-06-18 03:52, Mafi wrote:
> Am 18.06.2011 10:36, schrieb Jonathan M Davis:
> > The main purpose of toStringz is to turn a string into a char* which can
> > be passed to a C function. C doesn't have immutable, and I don't see any
> > gain in having toStringz return an immutable(char)* rather than a
> > const(char)*. Does anyone know why it returns an immutable rather than a
> > const? What possible benefit does that have?
> > 
> > - Jonathan M Davis
> 
> If anyone ever writes a function which needs immutable chars, toStringz
> just wroks for it. And functions which take const(char)* can get
> immutable(char)* without problems. (If not, that is bug, I hope)

Good point. I should have thought of that. I wouldn't expect many functions to 
be taking immutable(char)* though, since the main reason to use char* is to 
interface with C. But it doesn't hurt to make it immutable, I guess. It's more 
flexible even if that flexibility is almost always useless. But since that 
flexibility doesn't cost anything, we might as well have it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list