global vs context variable

monarch_dodra monarchdodra at gmail.com
Wed Dec 11 00:30:54 PST 2013


On Wednesday, 11 December 2013 at 08:21:33 UTC, luka8088 wrote:
> Hi everyone!
>
> I would like to address the issue of global variables (or 
> states). In
> general my opinion is that they are bad solely because they (in 
> most
> cases) lack the ability of alternative values (or states) or 
> ability to
> alter them in user friendly way.
>
> For example, take write function from std.stdio. For a third 
> party
> function that uses write to output to screen user is unable to 
> redirect
> that output to, for example, file without altering third party
> function's body.

"write" is really just a global helper/shortcut function that 
calls "stdout.write". If the user wants to customize this, then 
it's really no more complicated than doing a write to a named 
stream, which represents the global out, which may or may not be 
stdout:

auto myGlobalOut = stdout;
myGlobalOut.write(); //Writes to console (technically, "standard 
output")

//Change the global output context
myGlobalOut = File("out.txt", "w");
myGlobalOut.write(); //Writes to file

--------

I'm not really sure I understood the rest of what you posted 
though, so I can't make any comments on that.


More information about the Digitalmars-d mailing list