SumRange

Paul Backus snarwin at gmail.com
Tue Sep 30 21:09:52 UTC 2025


On Wednesday, 24 September 2025 at 00:12:40 UTC, monkyyy wrote:
> ```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
> }
> ```

`std.range.interfaces` already does this:

```d
import std.range.interfaces;
import std.range;
import std.algorithm;
import std.stdio;

void main()
{
     InputRange!int foo;

     auto bar1 = iota(10);
     foo = bar1.inputRangeObject;
     foreach (i; foo)
         writeln(i);

     auto bar2 = [1, 2, 3].map!(a => a);
     foo = bar2.inputRangeObject;
     foreach (i; foo)
         writeln(i);

     auto bar3 = only(1);
     foo = bar3.inputRangeObject;
     foreach (i; foo)
         writeln(i);
}
```


More information about the Digitalmars-d mailing list