std.locale

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Mar 1 21:47:46 PST 2009


Walter Bright wrote:
> Andrei Alexandrescu wrote:
>> Well I was thinking a global reference might be handy for people who 
>> e.g. want to set the locale once and then be done with it.
> 
> That's what I was objecting to!
> 
>> I think only a few apps actually manipulate multiple locales 
>> simultaneously. Most would just want to load the locale present on the 
>> user's computer and then use it.
> 
> User settable global state is eeevil.

I am thinking of a better form using scope-based locale usage. Consider:

class Locale { ... }
struct LocaleContext {
     this(Locale value);
     ~this();
     private Locale value();
     alias value this;
     ...
}

People wouldn't have access to a global Locale object. They can, 
however, create LocaleContext objects. Such objects set the current 
locale to user's locale in the constructor and restore the previous 
locale in the destructor.

That way use of locales follows use of scopes and the long-distance 
dependency created by globals is largely diminished.

An application just needing to create a LocaleContext upon loading and 
be done with it can create its own LocaleContext inside e.g. main(). A 
more sophisticated app may manage multiple locale contexts and put them 
in action as it needs. It's really flexible, and without promoting bad 
programming styles.


Andrei



More information about the Digitalmars-d mailing list