How is chunkBy supposed to behave on copy

Dukc ajieskola at gmail.com
Wed Mar 18 14:57:41 UTC 2020


```
void main()
{	import std;
	auto chunks = [14, 16, 18, 20, 22, 20, 18, 16]
	.chunkBy!((a, b) => a / 10 == b / 10);
	
	//[[14, 16, 18], [20, 22, 20], [18, 16]]
	chunks
	.map!(chunk => chunk.array)
	.array
	.writeln;
	
	//[]
	chunks
	.map!(chunk => chunk.array)
	.array
	.writeln;
}
```

This is how chunkBy[1] currently behaves when copying. It 
essentially behaves like a reference range: it will only save 
it's state when `save` is explicitly called, not otherwise, even 
if the chunked source range is a forward range with value 
semantics.

As most Phobos ranges preserve the value semantics of their 
source ranges, this is an odd exception, especially as the 
documentation says nothing about it.

So the question is, how should it behave? I have looked at the 
implementation, it should be trivial to have it to use value 
semantics. On the oher hand, someone may rely on the present 
behaviour.

[1] https://dlang.org/phobos/std_algorithm_iteration.html#.chunkBy


More information about the Digitalmars-d mailing list