fixedstring: a @safe, @nogc string type

Paul Backus snarwin at gmail.com
Tue Jan 11 19:38:29 UTC 2022


On Tuesday, 11 January 2022 at 17:55:28 UTC, H. S. Teoh wrote:
> Generally, I'd advise not conflating your containers with 
> ranges over your containers: I'd make .opSlice return a 
> traditional D slice (i.e., const(char)[]) instead of a 
> FixedString, and just require writing `[]` when you need to 
> iterate over the string as a range:
>
> 	FixedString!64 mystr;
> 	foreach (ch; mystr[]) { // <-- iterates over const(char)[]
> 		...
> 	}
>
> This way, no redundant copying of data is done during iteration.

It already does this. In D2, `[]` is handled by a zero-argument 
`opIndex` overload, not by `opSlice`. [1] FixedString has such an 
overload [2], and it does, in fact, return a slice.

[1] https://dlang.org/spec/operatoroverloading.html#slice
[2] 
https://github.com/Moth-Tolias/fixedstring/blob/v1.0.0/source/fixedstring.d#L105


More information about the Digitalmars-d-announce mailing list