Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 16, 2014 What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
I've looked at std.stream, but it says in big red scary letters that I probably shouldn't be using it. Is there a suitable replacement? If not, I'd just roll my own and provide a couple of templates to automatically generate wrappers for them. |
April 16, 2014 Re: What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hannes Steffenhagen | On Wednesday, 16 April 2014 at 21:05:30 UTC, Hannes Steffenhagen wrote:
> I've looked at std.stream, but it says in big red scary letters that I probably shouldn't be using it. Is there a suitable replacement? If not, I'd just roll my own and provide a couple of templates to automatically generate wrappers for them.
Have you looked into input and output ranges? Without knowing the specific details of what you need, ranges would be the way to go.
David
|
April 17, 2014 Re: What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | I wanna write a parser that can read the input text from an arbitrary source. That's going to be files for most of the time, but I don't want to unnecessarily limit it. It's a line based format, so I supposed InputRange!string would do in this case? |
April 17, 2014 Re: What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hannes Steffenhagen | On Thursday, 17 April 2014 at 08:45:29 UTC, Hannes Steffenhagen wrote: > I wanna write a parser that can read the input text from an arbitrary source. That's going to be files for most of the time, but I don't want to unnecessarily limit it. It's a line based format, so I supposed InputRange!string would do in this case? I would recommend writing a range using a struct instead of inheriting from InputRange. See here for an introduction to ranges: http://ddili.org/ders/d.en/ranges.html |
April 18, 2014 Re: What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | I've never said anything about inheriting from InputRange, just that I'd take an InputRange as a parameter. Anyway I changed my signature to Result parse(T)(T lines) if(isInputRange!T && is(ElementType!T == string)); Still not sure if that's the best way to do it, but it definitely is the one that proved to be the least hassle to use for my simple test cases. |
April 18, 2014 Re: What's the current phobos way to define generic data sources and sinks? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hannes Steffenhagen | On Fri, Apr 18, 2014 at 01:30:00AM +0000, Hannes Steffenhagen via Digitalmars-d-learn wrote: > I've never said anything about inheriting from InputRange, just that I'd take an InputRange as a parameter. Anyway I changed my signature to > > Result parse(T)(T lines) if(isInputRange!T && is(ElementType!T == > string)); > > Still not sure if that's the best way to do it, but it definitely is the one that proved to be the least hassle to use for my simple test cases. Please note that InputRange, as defined in std.range, is a *wrapper* class, intended for use in handling runtime polymorphism of ranges. It is not to be confused with the *concept* of an input range, which can be any type that provides the range API. The general practice in D is to use a template parameter for functions that take range parameters, so that the function can be instantiated for any type that satisfies the range API (including InputRange). InputRange generally is used by the code that *creates* the range, when it needs to be able to switch between different range types at runtime. T -- This sentence is false. |
Copyright © 1999-2021 by the D Language Foundation