static class

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 17 14:51:55 PST 2013


On Sunday, February 17, 2013 14:47:12 H. S. Teoh wrote:
> On Sun, Feb 17, 2013 at 11:46:24PM +0100, Andrej Mitrovic wrote:
> > On 2/17/13, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> > > A class that can't be instantiated has a private constructor.
> > 
> > An alternative to that is:
> > 
> > final abstract class C
> > {
> > 
> >     static:
> > }
> > 
> > You can't derive from it and you can't instantiate it.
> 
> Nice idiom!
> 
> This could be D's equivalent to C++ namespaces. ;-)

It's the closest that we've got, and Phobos uses it in a couple of places 
(like std.datetime.Clock), but it gets complained about quite a bit every time 
it comes up (particularly with regards to std.datetime.Clock). So, while it's 
useful upon occasion and some people like it, it's generally shunned. For the 
most part, it's considered better to just declare a free function and be done 
with it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list