www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing a byLine as an argument to InputRange

reply Jeff <massung gmail.com> writes:
I have a class where I'd like to supply it with an 
InputRange!string. Yet, for the life of me I can't seem to pass 
to it a File.byLine, even though the documentation states it's an 
InputRange.

```
class Foo {
   private InputRange!string source;

   this(InputRange!string s) {
     source = s;
   }

   // do stuff with it
}

new Foo(File("stuff.txt").byLine);

// Error constructor Foo.this(InputRange!string) is not callable 
using argument types (ByLineImpl!(char, char))
```

I have to believe this is possible. I'm thinking maybe I need to 
template the constructor with something like:

```
this(R)(R s) if (isInputRange!R)
```

But, then I seem to have 2 problems:

1. I haven't ensured it's an InputRange!string
2. I've just punted the problem up a level, because my member 
variable doesn't work.

Any help appreciated. Thanks!
May 13
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 13 May 2021 at 17:07:51 UTC, Jeff wrote:
 I have a class where I'd like to supply it with an 
 InputRange!string. Yet, for the life of me I can't seem to pass 
 to it a File.byLine, even though the documentation states it's 
 an InputRange.
byLine is not a range of string. It is a range of mutable char[]. I think byLineCopy yields strings though, try that. And if it doesn't work mabye `InputRange!(char[])` or similar might work.
May 13
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/13/21 10:07 AM, Jeff wrote:

 I have a class where I'd like to supply it with an InputRange!string.
 Yet, for the life of me I can't seem to pass to it a File.byLine
As Adam said, your range elements need to be converted to string e.g. with 'text' (the same as to!string). However, you must also create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject(): import std.range; import std.stdio; import std.algorithm; import std.conv; class Foo { private InputRange!string source; this(InputRange!string s) { source = s; } // do stuff with it } void main() { new Foo(inputRangeObject(File("stuff.txt").byLine.map!text)); } Ali
May 13
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 5/13/21 11:29 AM, Ali =C3=87ehreli wrote:

 create a=20
 dynamic InputRange!string object for dynamic polymorphism that=20
 InputRange provides. And that's achieved by function inputRangeObject()=
: Some examples: =20 http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.polymorphism,%= 20run-time Ali
May 13
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 13 May 2021 at 18:31:41 UTC, Ali Çehreli wrote:
 On 5/13/21 11:29 AM, Ali Çehreli wrote:

 create a dynamic InputRange!string object for dynamic 
 polymorphism that InputRange provides. And that's achieved by 
 function inputRangeObject():
Some examples: http://ddili.org/ders/d.en/ranges_more.html#ix_ranges_more.polymorphism,%20run-time Ali
This book has saved my life many times! ☀
May 13
prev sibling parent Jeff <massung gmail.com> writes:
On Thursday, 13 May 2021 at 18:29:08 UTC, Ali Çehreli wrote:
 On 5/13/21 10:07 AM, Jeff wrote:

 I have a class where I'd like to supply it with an
InputRange!string.
 Yet, for the life of me I can't seem to pass to it a
File.byLine As Adam said, your range elements need to be converted to string e.g. with 'text' (the same as to!string). However, you must also create a dynamic InputRange!string object for dynamic polymorphism that InputRange provides. And that's achieved by function inputRangeObject():
Thank you all. I'm up and running. ;-)
May 13