digitalmars.D.learn - Passing a byLine as an argument to InputRange
- Jeff (26/26) May 13 2021 I have a class where I'd like to supply it with an
- Adam D. Ruppe (4/8) May 13 2021 byLine is not a range of string. It is a range of mutable char[].
- =?UTF-8?Q?Ali_=c3=87ehreli?= (20/22) May 13 2021 As Adam said, your range elements need to be converted to string e.g.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/10) May 13 2021 :
- Imperatorn (2/9) May 13 2021 This book has saved my life many times! ☀
- Jeff (2/12) May 13 2021 Thank you all. I'm up and running. ;-)
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 2021
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 2021
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.byLineAs 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 2021
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 2021
On Thursday, 13 May 2021 at 18:31:41 UTC, Ali Çehreli wrote:On 5/13/21 11:29 AM, Ali Çehreli wrote:This book has saved my life many times! ☀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
May 13 2021
On Thursday, 13 May 2021 at 18:29:08 UTC, Ali Çehreli wrote:On 5/13/21 10:07 AM, Jeff wrote:Thank you all. I'm up and running. ;-)I have a class where I'd like to supply it with anInputRange!string.Yet, for the life of me I can't seem to pass to it aFile.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():
May 13 2021