SumRange

monkyyy crazymonkyyy at gmail.com
Wed Sep 24 00:12:40 UTC 2025


```d
import std;
struct sumrange(E){
	//void* me;
	//TypeInfo meta;
	E delegate() front_;
	E front()=>front_();
	void delegate() popFront_;
	void popFront()=>popFront_();
	bool delegate() empty_;
	bool empty()=>empty_();
	void opAssign(T)(ref T t){
		//me=cast(void*)&t;
		static assert(is(typeof(t.front())==E));
		front_=cast(E delegate())&t.front;
		popFront_=cast(void delegate())&t.popFront;
		empty_=cast(bool delegate())&t.empty;
	}
}

unittest{
	sumrange!int foo;
	auto bar=iota(int(10));
	foo=bar;
	foreach(i;foo){
		i.writeln;
	}
	auto bar2=[1,2,3].map!(a=>a);
	foo=bar2;
	foreach(i;foo){
		i.writeln;
	}
	auto bar3=only(1);
	foo=bar3;
	foreach(i;foo){
		i.writeln;
	}
	//foo=iota(10.0);//Error: static assert:  `is(double == int)` is 
false
}
```

Using sumtype or choice to get ranges to actually work in dense 
metaprogramming is very very hard; this looks like it will just 
work. Id ship it. Maybe aim for `SumRange!(ForwardRange!int)`, 
`SumRange!(BidirectionalAssignable!float)` to match upstream 
style guide


More information about the Digitalmars-d mailing list