www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static this

reply Max Samuha <maxter i.com.ua> writes:
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		
	}
}

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?
Oct 06 2006
next sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
On Fri, 06 Oct 2006 06:01:09 -0400, Max Samuha <maxter i.com.ua> 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		
 	}
 }

I also asked about this before and it's not a bug, "static" and "this" are supposed to be together, like static assert.
 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 you're right; my suggestion was to rename main() to this() :)
Oct 06 2006
next sibling parent Derek Parnell <derek psyc.ward> writes:
On Fri, 06 Oct 2006 06:40:46 -0400, Chris Miller wrote:

 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 you're right; my suggestion was to rename main() to this() :)

But each module can have it own constructor method and they can't all be 'main'. -- Derek Parnell Melbourne, Australia "Down with mediocrity!"
Oct 06 2006
prev sibling parent reply Max Samuha <maxter i.com.ua> writes:
On Fri, 06 Oct 2006 06:40:46 -0400, "Chris Miller"
<chris dprogramming.com> wrote:

On Fri, 06 Oct 2006 06:01:09 -0400, Max Samuha <maxter i.com.ua> 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		
 	}
 }

I also asked about this before and it's not a bug, "static" and "this" are supposed to be together, like static assert.

Then the compiler should definitely disallow 'this' in a static block giving a good compile time error message?
 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 you're right; my suggestion was to rename main() to this() :)

There may be 'this' in different modules. How the entry point would be determined then? Or please give me a link to the thread where your suggestion was discussed.
Oct 06 2006
parent "Chris Miller" <chris dprogramming.com> writes:
On Fri, 06 Oct 2006 07:30:18 -0400, Max Samuha <maxter i.com.ua> wrote:

 Then the compiler should definitely disallow 'this' in a static block
 giving a good compile time error message?

 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 you're right; my suggestion was to rename main() to this() :)

There may be 'this' in different modules. How the entry point would be determined then? Or please give me a link to the thread where your suggestion was discussed.

There would only be one module-level 'this', the rest would be 'static this'.
Oct 06 2006
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
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.
Oct 06 2006
parent reply Max Samuha <maxter i.com.ua> writes:
On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
<hasan.aljudy 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.
Oct 06 2006
parent reply Max Samuha <maxter i.com.ua> writes:
On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter i.com.ua>
wrote:

On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
<hasan.aljudy 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?
Oct 06 2006
next sibling parent Ary Manzana <asterite gmail.com> writes:
Max Samuha wrote:
 On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter i.com.ua>
 wrote:
 
 On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
 <hasan.aljudy 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		
 	}
 }

"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?

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.

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).
Oct 06 2006
prev sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Max Samuha wrote:
 On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter i.com.ua>
 wrote:
 
 
On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
<hasan.aljudy 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
Oct 06 2006
parent reply Max Samuha <maxter i.com.ua> writes:
On Fri, 06 Oct 2006 16:05:07 -0500, Chris Nicholson-Sauls
<ibisbasenji gmail.com> wrote:

Max Samuha wrote:
 On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter i.com.ua>
 wrote:
 
 
On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
<hasan.aljudy 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? class Test { static: this(){}; // make it static ctor, not instance. it's confusing! void foo(); }
Oct 07 2006
next sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Max Samuha wrote:
 
 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?
 
 class Test
 {
 static:
   this(){}; // make it static ctor, not instance.  it's confusing!
   void foo();	 
 }

My wild guess would be that the "static" significantly changes the functionality of "this()" so it's much more than just an attribute.
Oct 07 2006
prev sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Max Samuha wrote:
 On Fri, 06 Oct 2006 16:05:07 -0500, Chris Nicholson-Sauls
 <ibisbasenji gmail.com> wrote:
 
 Max Samuha wrote:
 On Fri, 06 Oct 2006 17:24:35 +0300, Max Samuha <maxter i.com.ua>
 wrote:


 On Fri, 06 Oct 2006 06:53:08 -0600, Hasan Aljudy
 <hasan.aljudy 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		
 	}
 }

"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?

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.

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?

# 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.
Oct 07 2006