static this

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Sat Oct 7 07:00:22 PDT 2006


Max Samuha wrote:
> On Fri, 06 Oct 2006 16:05:07 -0500, Chris Nicholson-Sauls
> <ibisbasenji at gmail.com> wrote:
> 
>> Max Samuha wrote:
>>> 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?
>> Would cause a problem with the continuous attribute syntax...
>>
>> # class Test {
>> # static:
>> #   static this() {}
>> #   void foo () {}
>> # }
>>
>> I'd say make it a non-blocking warning, rather than an error.
>>
>> -- Chris Nicholson-Sauls
> 
> Error, warning, anything to let me know about my static constructor
> being compiled as instance one.
> 
> Btw, i still can't see the reason why D doesn't treat 'static' as an
> attribute of 'this' like other static methods? Could anybody explain?

Who knows what the explanation might be. But it helps to look at static 
this as staticthis.



More information about the Digitalmars-d mailing list