static this

Ary Manzana asterite at gmail.com
Fri Oct 6 09:48:53 PDT 2006


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?

In some recent post I told it should be great if the compiler could tell 
if a modifier is redundant. I've seen the parser code and it seems 
pretty easy to implement.

Try with any modifier (abstract abstract, public private, even abstract 
final) and you'll get no error (of course, you won't be able to 
instantiate that class, the compiler thinks it's abstract).



More information about the Digitalmars-d mailing list