why won't byPair work with a const AA?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 2 08:52:22 PDT 2017


On Wed, Aug 02, 2017 at 08:20:23AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote:
> On 8/1/17 7:44 PM, H. S. Teoh via Digitalmars-d-learn wrote:
[...]
> You can iterate a const AA, but if you want to iterate a non-const AA,
> you need a different type.
> 
> For instance, if your AA is int*[string], you will get const(int*) out
> of it, which may not be what you want.
> 
> Unless your range is a slice or a pointer, then you can't do it
> properly without having separate types for const, immutable, mutable
> ranges. You can use templates to build them, but it's not
> straightforward or pleasant.
[...]

Hmm. This seems like a perfect use case for inout, though I'm not
confident the current implementation of inout can handle this. You'd do
something like this:

	auto byPair(AA)(inout(AA) aa)
	{
		struct Result
		{
			inout(Slot)* current;
			... // range primitives here
		}
		return Result(aa);
	}

What I'm unsure of is whether the current implementation of inout can
handle the inout inside the definition of Result.


T

-- 
Trying to define yourself is like trying to bite your own teeth. -- Alan Watts


More information about the Digitalmars-d-learn mailing list