evenChunks on a string - hasLength constraint fails?

amarillion mpvaniersel at gmx.com
Tue Mar 14 08:21:00 UTC 2023


Hey

I'm trying to split a string down the middle. I thought the 
function std.range.evenChunks would be perfect for this:

```
#!/usr/bin/env -S rdmd -I..

import std.range;

void main() {
	string line = "abcdef";
	auto parts = evenChunks(line, 2);
	assert(parts == ["abc", "def"]);
}
```

But I'm getting a compiler error:

```
/usr/include/dmd/phobos/std/range/package.d(8569):        
Candidate is: `evenChunks(Source)(Source source, size_t 
chunkCount)`
   with `Source = string`
   whose parameters have the following constraints:
   `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
`    isForwardRange!Source
   > hasLength!Source
`  `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
./test.d(7):        All possible candidates are marked as 
`deprecated` or `@disable`
```

I'm trying to understand why this doesn't work. I don't really 
understand the error. If I interpret this correctly, it's missing 
a length attribute on a string, but shouldn't length be there?



More information about the Digitalmars-d-learn mailing list