On Monday, 4 August 2014 at 22:00:18 UTC, splatterdash wrote:
``` File f = File("input_file") // detect gzip ... if (isGzip)
{
auto fileIter = new MyFileReader!GzipIterator(f)
}
else
{
auto fileIter = new MyFileReader!NormalIterator(f)
}
foreach(string line; fileIter) { // do things } ```
(Added braces for clarity.)
However I always get the compiler error `undefined identifier lineIter`.
You have two independent variables called fileIter. Both are only available in their respective scopes. Declare the variable before the `if` to get one variable with a wider scope: SomeCommonBaseTypeOfThoseMyFileReaderVariants fileIter; if (isGzip) fileIter = new MyFileReader!GzipIterator(f); else fileIter = new MyFileReader!NormalIterator(f);