On Monday, 4 August 2014 at 22:45:15 UTC, anonymous wrote:
On Monday, 4 August 2014 at 22:18:24 UTC, splatterdash wrote:
Indeed I do. I'm not sure which type I should use for the
common base type, though. MyFileReader is a templated class,
so using it plainly did not work. I also tried
`InputRange!string` to no avail despite `MyFileReader`
implementing the three InputRange requirement (popFront(),
front, and empty).
Any ideas on what I should as the class?
Let MyFileReader implement an interface that has the operations
you need. That interface can be std.range.InputRange!string, or
you can define your own.
Note that a type is an input range when it has the input range
primitives (front, popFront, empty), but it's only a
std.range.InputRange!T when it implements the interface in the
OOP sense: class C : InputRange!E {...}.
Phobos generally doesn't use InputRange, but templatizes
everything. You can go that way, too, and move the foreach loop
to a templated function:
void main()
{
File f = File("input_file")
// detect gzip ...
if (isGzip)
doThings(new MyFileReader!GzipIterator(f));
else
doThings(new MyFileReader!NormalIterator(f));
}
void doThings(I)(I fileIter)
{
foreach(string line; fileIter) {
// do things
}
}
That does it, thanks :)!