static this

Max Samuha maxter at i.com.ua
Fri Oct 6 08:53:44 PDT 2006


On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter at i.com.ua>
wrote:

>On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
><hasan.aljudy at gmail.com> wrote:
>
>>
>>
>>Max Samuha wrote:
>>> Two questions to the community:
>>> 
>>> 1. The following is obviously a bug?
>>> 
>>> class Test
>>> {
>>> 	static
>>> 	{
>>> 		this(){}; // defined not as static constructor but as
>>> instance constructor
>>> 
>>> 		... more static members here		
>>> 	}
>>> }
>>
>>As Chris already said, static is not really an attribute, it's just 
>>"static this".
>>
>>> 
>>> 2. Why module constructor must be attributed with static? All module
>>> level methods are already static so the attribute seems to be
>>> redundant. Make it optional?
>>> 
>>
>>I guess the reason is to be consistent. "static this" refers to a static 
>>constructor, i.e. a constructor that will be called at the beginning of 
>>the program's start.
>>Module constructors are static constructors, so "static this" is used.
>
>Ok, that's clear. Anyway, the compiler should issue an error if 'this'
>is used in static block. If not, it will be be confusing for newcomers
>who will be unlucky enough to use it the way I did.

And, IMO, this one shouldn't compile without complaint, either: 

class Test
{
	static
	{
		static this()
		{
			writefln("In static ctor");
		}
	}

	static static void foo()
	{
	}
}

void main()
{
}

How do you think?



More information about the Digitalmars-d mailing list