Why is opIndexAssign replaced by opSlice here?

Elmar chrehme at gmx.de
Sun Oct 17 22:52:27 UTC 2021

Hello Dear community.

I'd like to overload `opIndexAssign` for a struct which wraps 
around a generic array (so that it can't support `opIndex` due to 
unknown return type).

Broken down as much as possible this is the code:

import std.stdio : writeln;
import std.range : ElementType;

struct S {
	void opIndexAssign(X, RANGE)(X x, RANGE range)
		if (is(ElementType!RANGE : size_t))

	auto opSlice(size_t start, size_t end) {
		import std.range : iota;
		return iota(start, end);

void main()
	auto arr = new int[7];

	S s;
	s.opIndexAssign(arr, s.opSlice(1,4));  // works
	s[0..3] = arr[1..4];  // does not work, compiles to 
`s.opSlice(0,3) = arr[1..4]`

I'm clueless about why it wouldn't compile the last statement to 
`s.opIndexAssign(arr[1..4], s.opSlice(0,3))`.

Help appreciated :-)

More information about the Digitalmars-d-learn mailing list