"immutable string" vs "const string*"
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Sep 9 10:45:07 UTC 2018
On Sunday, September 9, 2018 2:49:56 AM MDT rikki cattermole via
> On 09/09/2018 8:41 PM, Christian Mayer wrote:
> > On Sunday, 9 September 2018 at 08:14:41 UTC, rikki cattermole wrote:
> >> Are you aware that a string is just an alias of immutable(char)?
> > Yes, I'm aware of that. But it's the same, for example, with just one
> > char. "immutable char" vs "const char*".
> > Or int, or any other data type. As of my current understanding "char"
> > will create a new variable and copy the content of the original to the
> > new variable. "char*" will just use the pointer. And "const char*" is
> > good for when not modifying. But I also can achieve the same using
> > "immutable char". But I'm not sure when better using "immutable char".
> > In C I would rather use a const pointer. But since I just started
> > learing D I'm not so sure because there are so many ways.
> Don't over think it. The only thing you're doing by micro-optimization
> is causing potential problems that wouldn't otherwise exist.
Yeah. I don't know if I've ever seen any D code take a string*. There's
really no reason to. If the string is just being passed in, then it's going
to be string. If you have an array of strings, then that's what you'd pass.
And if you want to mutate the string that's being passed in, then you'd pass
by ref. Just about the only time that I'd expect pointers to be used with
relation to strings in D is if you're calling C functions, and you need to
pass a null-terminated string.
- Jonathan M Davis
More information about the Digitalmars-d-learn