How To Dynamic Web Rendering?

Adam D. Ruppe destructionator at gmail.com
Mon May 23 09:57:06 PDT 2011


Also:

" other two errors say that "modulename.run" cannot be called with
argument type Cgi and that 0 arguments are expected for function type void "

When using the generic main mixin, the function you pass must
always take one argument: a Cgi object.

void yourFunctionHere(Cgi cgi) { }

When the mixin is compiled, it expands to something like this:


void main() {
   auto cgi = new Cgi;
   scope(exit) cgi.close();
   try {
        yourFunctionHere(cgi);
   } catch (Exception) {
        // log and maybe display the error then exit
   }
}


So the function you pass to the mixin is called with an argument:

        yourFunctionHere(cgi);


Which might be producing the error you saw, if your function
couldn't take the cgi object as it's own argument.




Why use the mixin at all? Writing main yourself might be simpler.

There's three reasons:

!) It saves you from writing the same thing over and over as your
   main function. This is a small benefit, but it's nice.

2) It can display errors to the browser in debug mode. This makes
   debugging easier. The message is right there.

3) This is the biggest one: if the main() function ever needs to
   change, your code doesn't.

Thanks to the GenericMain mixin, for example, you can switch to
using FastCGI or a built in webserver simply by recompiling with
a different version. None of your code has to change.


More information about the Digitalmars-d-learn mailing list