global vs context variable

luka8088 luka8088 at owave.net
Wed Dec 11 04:51:05 PST 2013


On 11.12.2013. 9:30, monarch_dodra wrote:
> 
> "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

Yes. That is exactly what this kind of approach is all about. In
practice I found a need to temporarily change myGlobalOut while keeping
in mind that recursion could be used and in most cases a stack was
required. You also need to make sure that stack is pop'ed properly (even
in case of throwing) so generally there is some work to be done. I think
that this kind of approach would be much more user friendly if a library
support was introduced.

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

Take MVC for example. Most frameworks either support global variables or
setting a controller properties for stuff that needs to be accessible
globally. I have seen database connections being a property of a
controller, which if you think about it makes no sense, but it was the
most pragmatical way to do it in order to make database connection
"globally" accessible without setting is as a global variable.



More information about the Digitalmars-d mailing list