I hope I didn't miss anything; I copied it from the book.

import std.stdio, std.exception;

interface Stat{
    void accumulate(double x);
    void postprocess();
    double result();
}

class Min : Stat{
    private double min = double.max;
    void accumulate(double x){
        if( x < min ){
        min = x;
        }
    }

    void postprocess(){ }
    double result(){
        return min;
    }
}

void main(string[] args){

     Stat[] stats;
     foreach(arg; args[1 .. $]){
               auto newStat = cast(Stat) Object.factory("stats." ~ arg);
          enforce(newStat, "Invalid statistics function: " ~ arg);
          stats ~= newStat;
      }
      for(double x; stdin.readf(" %s ", &x) == 1; ){
               foreach(s; stats){
                 s.accumulate(x);
        }
       }
      foreach(s; stats){
               s.postprocess();
         writeln(s.result());
      }
}


On Thu, Dec 23, 2010 at 6:04 PM, Andrei Alexandrescu <
[email protected]> wrote:

> On 12/23/10 5:50 PM, Caligo wrote:
>
>>
>>
>> On Thu, Dec 23, 2010 at 5:38 PM, Andrej Mitrovic
>> <[email protected] <mailto:[email protected]>> wrote:
>>
>>    On 12/24/10, Caligo <[email protected]
>>    <mailto:[email protected]>> wrote:
>>     > You got me excited, so I decided to give GDC another try.  I
>>    cloned the
>>     > repo, and using GCC 4.4.5, it compiled without errors.
>>     > I started following the examples in TDPL, but the Stat program on
>>    page 22
>>     > gives the following errors:
>>     >
>>     > t1.d:33: Error: void has no value
>>     > t1.d:33: Error: incompatible types for ((readf(" %s ",& x)) ==
>>    (1)): 'void'
>>     > and 'int'
>>     >
>>     > is there a typo in the code, or is this some kind of bug in GDC?
>>
>>    Use stdin.readf:
>>
>>    import std.exception, std.stdio;
>>    void main(string[] args) {
>>      for (double x; stdin.readf(" %s ", &x) == 1; ) {
>>      }
>>    }
>>
>>    The TDPL errata is here btw, that bug is listed:
>>    http://erdani.com/tdpl/errata/index.php?title=Main_Page
>>
>>
>>
>> great, that fixed the problem.  I was actually trying to find the errata
>>
>> too, so thanks for that.
>>
>> The example compiles but it throws an exception:
>>
>>  >> echo 3 4 6.6 7.7 33.4 | ./t1 Min Max Average
>> [email protected]@30
>>
>
> Would you mind posting the entire code? Thanks.
>
> Andrei
>

Reply via email to