www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - proposal: OOP wrapper for ranges for easy interfacing

reply Timothee Cour <thelastmammoth gmail.com> writes:
--089e011825f824a8cf04f3c0c35c
Content-Type: text/plain; charset=ISO-8859-1

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)));
}

--089e011825f824a8cf04f3c0c35c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Interfacing with ranges across libraries can be trick=
y 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 distribu=
te with di interface files.=A0<br>
</div><div>=A0<br></div><div>I&#39;d like to have something like the follow=
ing in phobos (see code below).<br></div><div>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 great=
ly benefit compilation time since it enables us to use thin di interface fi=
les.</div>
<div><br></div><div>----</div><div><br></div><div>module util.oop;</div><di=
v>import std.range;</div><div><br></div><div>interface InputRange(T){</div>=
<div>=A0 void popFront();</div><div>=A0 T front();</div><div>=A0 bool empty=
();</div>
<div>=A0 typeof(this) save(); //should actually have a whole OOP hierarchy =
to support the various range paradigms, with assert(0) when un-implemented<=
/div><div>}</div><div><br></div><div>//TODO:more generic, eg: to!InputRange=
</div>
<div>auto toInputRange(R)(R a)if(isInputRange!R){</div><div>=A0 alias T=3DE=
lementType!R;</div><div>=A0 class A:InputRange!T{</div><div>=A0 =A0 R a;</d=
iv><div>=A0 =A0 this(R a){</div><div>=A0 =A0 =A0 this.a=3Da;</div><div>=A0 =
=A0 }</div><div>=A0 =A0 void popFront(){a.popFront;}</div>
<div>=A0 =A0 T front(){return a.front;}</div><div>=A0 =A0 bool empty(){retu=
rn a.empty;}</div><div>=A0 =A0 A save(){return new A(a);}</div><div>=A0 }</=
div><div>=A0 return new A(a);</div><div>}</div><div><br></div><div>unittest=
{</div><div>
=A0 auto fun(InputRange!int a){//fun is *not* templated; works with any inp=
ut range whose element type is int.</div><div>=A0 =A0 return a.array;</div>=
<div>=A0 }</div><div>=A0 import std.algorithm:equal;</div><div>=A0 assert(f=
un(10.iota.map!(a=3D&gt;a*2).toInputRange).equal(iota(0,20,2)));</div>
<div>}</div><div><br></div></div>

--089e011825f824a8cf04f3c0c35c--
Mar 03 2014
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
see std.range.inputRangeObject and the associated interfaces

http://dlang.org/phobos/std_range.html#InputRange

it is templated on only the element type
Mar 03 2014
prev sibling next sibling parent "Brad Anderson" <eco gnuk.net> writes:
On Tuesday, 4 March 2014 at 04:59:12 UTC, Timothee Cour wrote:
 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.

Have you seen InputRange and inputRangeObject in std.range? http://dlang.org/phobos/std_range.html#.InputRange http://dlang.org/phobos/std_range.html#inputRangeObject
Mar 03 2014
prev sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--001a11c2019af5f9bb04f3c17c14
Content-Type: text/plain; charset=ISO-8859-1

Ah, thanks I should've searched more, that's exactly what I wanted.


On Mon, Mar 3, 2014 at 9:02 PM, Brad Anderson <eco gnuk.net> wrote:

 On Tuesday, 4 March 2014 at 04:59:12 UTC, Timothee Cour wrote:

 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.

http://dlang.org/phobos/std_range.html#.InputRange http://dlang.org/phobos/std_range.html#inputRangeObject

--001a11c2019af5f9bb04f3c17c14 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Ah, thanks I should&#39;ve searched more, that&#39;s exact= ly what I wanted.=A0</div><div class=3D"gmail_extra"><br><br><div class=3D"= gmail_quote">On Mon, Mar 3, 2014 at 9:02 PM, Brad Anderson <span dir=3D"ltr= ">&lt;<a href=3D"mailto:eco gnuk.net" target=3D"_blank">eco gnuk.net</a>&gt= ;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div class=3D"">On Tuesday, 4 March 2014 at = 04:59:12 UTC, Timothee Cour wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Interfacing with ranges across libraries can be tricky due to heavy use of<= br> templates. Example cases are when interfacing between D and C++ or when<br> using D shared libraries, that we would like to distribute with di<br> interface files.<br> <br> I&#39;d like to have something like the following in phobos (see code below= ).<br> The code makes an OOP wrapper for ranges, allowing one to use separate<br> compilation model without having to pull in all the source code if we want<= br> to use the library with arbitrary ranges of a given element type (see<br> unittest below). This could also greatly benefit compilation time since it<= br> enables us to use thin di interface files.<br> <br> </blockquote> <br></div> Have you seen InputRange and inputRangeObject in std.range?<br> <br> <a href=3D"http://dlang.org/phobos/std_range.html#.InputRange" target=3D"_b= lank">http://dlang.org/phobos/std_<u></u>range.html#.InputRange</a><br> <a href=3D"http://dlang.org/phobos/std_range.html#inputRangeObject" target= =3D"_blank">http://dlang.org/phobos/std_<u></u>range.html#inputRangeObject<= /a><br> </blockquote></div><br></div> --001a11c2019af5f9bb04f3c17c14--
Mar 03 2014