Weird UFC and opCall issues

ted via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 14:17:58 PST 2015


Darrell wrote:

> Seems when creating your own ranges they can't be a class.
> Must be a struct or Segmentation fault (core dumped) will follow.
> 
> This works as long as Test is a struct.
> 
> struct Test
> {
>    @property int front()
>    {
>      return 2;
>    }
> 
>    void popFront()
>    {
>    }
> 
>    enum bool empty = false;
> };
> 
> static assert(isInputRange!Test);
> 
> void mainx(){
>    Test x;
>    writeln(x.take(1));
> }


With classes, you need to create an instance, so (adjusting some of your 
previous code) this works:

import std.range;
import std.stdio;

class Test
{
   @property int front()
   {
     return 2;
   }

   void popFront()
   {
   }

   @property bool empty()
   {
     return false;
   }

};
static assert(isInputRange!Test);

void main(){
   ubyte [] p1;
    Test myTest = new Test();
    writeln(myTest.take(1));
}








More information about the Digitalmars-d-learn mailing list