[Bikeshed] getter/readonly mixin util with private/protected backing

Lutger lutger.blijdestijn at gmail.com
Thu Nov 12 00:16:10 PST 2009


Nick Sabalausky wrote:

> In my SemiTwist D Tools project (
> http://www.dsource.org/projects/semitwist ), I have a util mixin to DRY-ly
> create a publically-readonly property with backing storage (actually, I've
> had it for awhile, but I've been toying with it again recently).
> 
> 1. It can optionally give the backing storage any access level you want,
> but what I'm wondering is, what do you think would be a better default:
> private or protected?

Private for sure. 
 
> 2. As a secondary question, do you think 'getter', 'readonly' or something
> else would be the best name for it?

getter is the clearest imho.
 
> 3. There's an alternate version that creates a getter that, instead of
> returning the backing storage, works like this: On the first call, a
> user-supplied function is called that generates the value. This value is
> automatically cached and returned. Subsequent calls return the cached
> value until the private (or protected) 'cached' flag is cleared. What
> would be a good name for this util? 'getterLazy', 'getterCached',
> 'readonlyLazy', 'readonlyCached', etc..., or just simply overload the
> regular 'getter'/'readonly'/etc...?

What about passing both this and access level as a parameter to the 
template? 

Something like this perhaps:

mixin ( getter!("myFunc", "myInitializer", PROTECTED | LAZY) );

 
> 4. Do you think this util would be a useless waste, or potentially handy?
> 
> My main interest is Q #1, but I'm curious about opinions on the others
> too.
> 

Might be handy depending on the kind of things you are going to write.





More information about the Digitalmars-d mailing list