One way to deal with transient ranges & char[] buffers

monarch_dodra monarchdodra at gmail.com
Fri Aug 2 01:46:16 PDT 2013


On Friday, 2 August 2013 at 07:50:28 UTC, Jakob Ovrum wrote:
> Places in D that require `string` either do so because they 
> need the immutable guarantee or they do so out of error (e.g. 
> should have used a string of const characters instead). The 
> latter can of course be worked around, but the only *solution* 
> involves fixing the upstream code, so I'll assume we're 
> discussing the former case.

One of the problems is often the return type. For example, readln 
will return a string, because it is simply more convenient in end 
user code. The "standard" string format in D is "string", so 
that's what it returns.

However, if you call readln!(char[]), then you'll the *exact* 
same string, but of a non-immutable type.

This means that you *can* get the best out of both worlds, at no 
extra run-time cost. I think more functions should do this.

Without doing this, you face the eternal problem: Should I return 
a "string", to give my end user more guarantees, when in fact my 
char array is perfectly mutable, or should I return a char[], 
forcing my end user to make an idup(or an unsafe cast) if he 
actually needed a string?

It's a tough problem to tackle.


More information about the Digitalmars-d mailing list