Declaring run time variables

splatterdash via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 4 15:00:16 PDT 2014


Hello everyone,

I'm trying to write a command-line application that can detect 
whether the input file is gzipped or not. Sounds simple enough, 
but I can't seem to do it properly in D (this is my first foray 
to the language).

After checking whether the file is gzipped or not, I try to 
create a new instance of a class that is basically an InputRange 
returning each line of the input file:

```
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
}
```

However I always get the compiler error `undefined identifier 
lineIter`.

Now, my questions are:

1. What is causing the error? Is this caused by the compiler not 
being able to figure out what fileIter is at compile time?

2. I realize the current way of creating file handles may not be 
the best way to handle gzipped or non-gzipped file. Is there a 
better way to detect and create such file handles based on run 
time values?

Thanks in advance :).


More information about the Digitalmars-d-learn mailing list