isBidirectionalRange fails for unknown reasons

anonymous via Digitalmars-d-learn digitalmars-d-learn at
Wed Dec 16 13:40:44 PST 2015

On 16.12.2015 21:43, Jack Stouffer wrote:
> I'm trying to add a ReferenceBidirectionalRange range type to
> std.internal.test.dummyrange so I can test some range code I'm writing,
> but I've hit a wall and I'm not sure why. For some reason, the
> isBidirectionalRange check fails even though back and popBack are
> present. Any help here would be appreciated.
> class ReferenceForwardRange(T) : ReferenceInputRange!T
> {
>      this(Range)(Range r) if (isInputRange!Range) { super(r); }
>      final @property ReferenceForwardRange save()
>      {return new ReferenceForwardRange!T( _payload); }
> }
> class ReferenceBidirectionalRange(T) : ReferenceForwardRange!T
> {
>      this(Range)(Range r) if (isInputRange!Range) { super(r); }
>      final @property ref T back(){ return _payload.back; }
>      final void popBack(){ _payload.popBack(); }
> }

The `.save` primitive of forward ranges must return the very same type 
that the range has. But your ReferenceBidirectionalRange! returns 
a ReferenceForwardRange!T, because it's inherited. That makes 
isForwardRange!(ReferenceBidirectionalRange!T) fail, and everything that 
depends on it.

You can override `save` in ReferenceBidirectionalRange or try something 
clever like using a template this parameter:

@property auto save(this This)() {return new This( _payload);}

More information about the Digitalmars-d-learn mailing list