proposal: OOP wrapper for ranges for easy interfacing

Timothee Cour thelastmammoth at gmail.com
Mon Mar 3 20:59:00 PST 2014


Interfacing with ranges across libraries can be tricky due to heavy use of
templates. Example cases are when interfacing between D and C++ or when
using D shared libraries, that we would like to distribute with di
interface files.

I'd like to have something like the following in phobos (see code below).
The code makes an OOP wrapper for ranges, allowing one to use separate
compilation model without having to pull in all the source code if we want
to use the library with arbitrary ranges of a given element type (see
unittest below). This could also greatly benefit compilation time since it
enables us to use thin di interface files.

----

module util.oop;
import std.range;

interface InputRange(T){
  void popFront();
  T front();
  bool empty();
  typeof(this) save(); //should actually have a whole OOP hierarchy to
support the various range paradigms, with assert(0) when un-implemented
}

//TODO:more generic, eg: to!InputRange
auto toInputRange(R)(R a)if(isInputRange!R){
  alias T=ElementType!R;
  class A:InputRange!T{
    R a;
    this(R a){
      this.a=a;
    }
    void popFront(){a.popFront;}
    T front(){return a.front;}
    bool empty(){return a.empty;}
    A save(){return new A(a);}
  }
  return new A(a);
}

unittest{
  auto fun(InputRange!int a){//fun is *not* templated; works with any input
range whose element type is int.
    return a.array;
  }
  import std.algorithm:equal;
  assert(fun(10.iota.map!(a=>a*2).toInputRange).equal(iota(0,20,2)));
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140303/4ca34d66/attachment.html>


More information about the Digitalmars-d mailing list