general questions about static this() at module level

sarn via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 30 22:42:16 PDT 2016


On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote:
> First, are there any other languages that has this feature?  
> The few I know, certainly don't.

I've seen hacks to do the same thing in C++.  They're not pretty, 
though.

> And how would you compare and contrast these this(s) with those 
> of structs and classes?
> Like, are they especially good for certain D idioms, particular 
> cases, or are they good in general?

Class/struct static constructors are good for initialising 
class/struct static data.  Module static constructors are good 
for initialising module "static data" (i.e., globals).  They're 
especially handy for initialising immutable global data (which is 
the kind of global data D encourages).

BTW, immutable data is shared between threads, so you should use 
"shared static this" to initialise it.  Regular static 
constructors are executed per thread.


More information about the Digitalmars-d-learn mailing list