www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - mixing static if and if

reply Thomas Kuehne <thomas-dloop kuehne.this-is-spam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Is the following code legal, illegal or undefined?

void test(){
	const int a;
	int b;
	static if(a==3){
		...
	} else if(b==2){
		...
	}
}

http://dstress.kuehne.cn/undefined/static_if_05_A.d
http://dstress.kuehne.cn/undefined/static_if_05_B.d

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFCqczs3w+/yD4P9tIRAo6kAJwOP8qYWovTFgJWhWLx+ZLlo5K4ygCgnplj
vS8vTWaFfxXNFUKdTIkhJaY=
=LdZW
-----END PGP SIGNATURE-----
Jun 10 2005
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
Isn't that the same as:

void test()
{
	const int a;
	int b;

	static if (a == 3)
	{
		// ...
	}
	else
	{
		if (b == 2)
		{
			// ...
		}
	}
}

Which is totally legal...?

-[Unknown]


 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Is the following code legal, illegal or undefined?
 
 void test(){
 	const int a;
 	int b;
 	static if(a==3){
 		...
 	} else if(b==2){
 		...
 	}
 }
 
 http://dstress.kuehne.cn/undefined/static_if_05_A.d
 http://dstress.kuehne.cn/undefined/static_if_05_B.d
 
 Thomas
 
 
 -----BEGIN PGP SIGNATURE-----
 
 iD8DBQFCqczs3w+/yD4P9tIRAo6kAJwOP8qYWovTFgJWhWLx+ZLlo5K4ygCgnplj
 vS8vTWaFfxXNFUKdTIkhJaY=
 =LdZW
 -----END PGP SIGNATURE-----

Jun 10 2005
next sibling parent reply Thomas Kuehne <thomas-dloop kuehne.this-is-spam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Unknown W. Brackets schrieb am Fri, 10 Jun 2005 10:59:58 -0700:
 Isn't that the same as:

 void test()
 {
 	const int a;
 	int b;

 	static if (a == 3)
 	{
 		// ...
 	}
 	else
 	{
 		if (b == 2)
 		{
 			// ...
 		}
 	}
 }

 Which is totally legal...?

Ok, a bit more complex: void test() { cont int a; int b; if (b==1){ // ... }else static if(a==2){ // ... int c; }else if(b==3){ // ... } } Can't be rewritten in the same way. c and b are in the same scope but would be in different scopes in the rewrite. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCqeDE3w+/yD4P9tIRAsJYAKCD/QWeeuOIXgOAIk7pdGVDn4UTswCfQ0zR rKlVcgTibYPH3trN1A0pGZw= =z3H3 -----END PGP SIGNATURE-----
Jun 10 2005
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
Well, imho, that *should* be exactly the same as:

void test()
{
	const int a;
	int b;

	if (b == 1)
	{
	}
	else // <-- this is the else I'm talking about.
	{
		static if (a == 2)
		{
			int c; // <-- visible inside whole else above.
		}
		else
		{
			// One couldn't use c here, though.
			if (b == 3)
			{
			}
		}
	}
}

If it's not currently, I'd say that it's the bug.

-[Unknown]


 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Unknown W. Brackets schrieb am Fri, 10 Jun 2005 10:59:58 -0700:
 
Isn't that the same as:

void test()
{
	const int a;
	int b;

	static if (a == 3)
	{
		// ...
	}
	else
	{
		if (b == 2)
		{
			// ...
		}
	}
}

Which is totally legal...?

Ok, a bit more complex: void test() { cont int a; int b; if (b==1){ // ... }else static if(a==2){ // ... int c; }else if(b==3){ // ... } } Can't be rewritten in the same way. c and b are in the same scope but would be in different scopes in the rewrite. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCqeDE3w+/yD4P9tIRAsJYAKCD/QWeeuOIXgOAIk7pdGVDn4UTswCfQ0zR rKlVcgTibYPH3trN1A0pGZw= =z3H3 -----END PGP SIGNATURE-----

Jun 10 2005
parent reply Thomas Kuehne <thomas-dloop kuehne.this-is-spam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Unknown W. Brackets schrieb am Fri, 10 Jun 2005 13:51:00 -0700:
 Well, imho, that *should* be exactly the same as:

 void test()
 {
 	const int a; 
 	int b;

 	if (b == 1)
 	{
 	}
 	else // <-- this is the else I'm talking about.
 	{
 		static if (a == 2)
 		{
 			int c; // <-- visible inside whole else above.
 		}
 		else
 		{
 			// One couldn't use c here, though.
 			if (b == 3)
 			{
 			}
 		}
 	}

 }

 If it's not currently, I'd say that it's the bug.

Now add "b+=c;" to the end. Before the rewrite b and c were declared in the same scope. After the rewrite b and c are declared in different scopes. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCqgFm3w+/yD4P9tIRAg3lAKCJdhpKjjLELy2oCvmxcLtYymwDKQCgxZ9g g2X6c3SvzLTvJXIvkAOMmF4= =xXMq -----END PGP SIGNATURE-----
Jun 10 2005
parent reply Thomas Kuehne <thomas-dloop kuehne.this-is-spam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Kuehne schrieb am Fri, 10 Jun 2005 23:08:54 +0200:
 Unknown W. Brackets schrieb am Fri, 10 Jun 2005 13:51:00 -0700:
 Well, imho, that *should* be exactly the same as:

 void test()
 {
 	const int a; 
 	int b;

 	if (b == 1)
 	{
 	}
 	else // <-- this is the else I'm talking about.
 	// **A** {
 		static if (a == 2)
 		{
 			int c; // <-- visible inside whole else above.
 		}
 		else
 		{
 			// One couldn't use c here, though.
 			if (b == 3)
 			{
 			}
 		}
 	// **B** }

 }

 If it's not currently, I'd say that it's the bug.

Now add "b+=c;" to the end. Before the rewrite b and c were declared in the same scope. After the rewrite b and c are declared in different scopes.

Allright, the scopeless "static if" can be a bit confusing at first. Remove the brackets A and B and everything turns legal and documented. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCqgan3w+/yD4P9tIRAqE1AJ9uEdyJ6mNaDVoATcQ/8Nx5hVlkSwCfRjl2 SYtOdJSv5ZQRpkxaVv1hWb4= =4A/d -----END PGP SIGNATURE-----
Jun 10 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Thomas Kuehne wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Thomas Kuehne schrieb am Fri, 10 Jun 2005 23:08:54 +0200:
 
Unknown W. Brackets schrieb am Fri, 10 Jun 2005 13:51:00 -0700:

Well, imho, that *should* be exactly the same as:

void test()
{
	const int a; 
	int b;

	if (b == 1)
	{
	}
	else // <-- this is the else I'm talking about.
	// **A** {
		static if (a == 2)
		{
			int c; // <-- visible inside whole else above.
		}
		else
		{
			// One couldn't use c here, though.
			if (b == 3)
			{
			}
		}
	// **B** }

b+=c;
}

If it's not currently, I'd say that it's the bug.

Now add "b+=c;" to the end. Before the rewrite b and c were declared in the same scope. After the rewrite b and c are declared in different scopes.

Allright, the scopeless "static if" can be a bit confusing at first. Remove the brackets A and B and everything turns legal and documented.

Legal? The outermost else block is of a normal if, not a static if, and therefore it creates a scope. Why should whether the body is a BlockStatement or not make a difference? It makes no sense, since whether or not c is declared would depend at runtime on whether b == 1. Documented? Where? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 14 2005
parent reply Thomas Kuehne <thomas-dloop kuehne.this-is-spam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stewart Gordon schrieb am Tue, 14 Jun 2005 10:43:04 +0100:
 Thomas Kuehne wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Thomas Kuehne schrieb am Fri, 10 Jun 2005 23:08:54 +0200:
 
Unknown W. Brackets schrieb am Fri, 10 Jun 2005 13:51:00 -0700:

Well, imho, that *should* be exactly the same as:

void test()
{
	const int a; 
	int b;

	if (b == 1)
	{
	}
	else // <-- this is the else I'm talking about.
	// **A** {
		static if (a == 2)
		{
			int c; // <-- visible inside whole else above.
		}
		else
		{
			// One couldn't use c here, though.
			if (b == 3)
			{
			}
		}
	// **B** }

b+=c;
}

If it's not currently, I'd say that it's the bug.

Now add "b+=c;" to the end. Before the rewrite b and c were declared in the same scope. After the rewrite b and c are declared in different scopes.

Allright, the scopeless "static if" can be a bit confusing at first. Remove the brackets A and B and everything turns legal and documented.

Legal? The outermost else block is of a normal if, not a static if, and therefore it creates a scope. Why should whether the body is a BlockStatement or not make a difference? It makes no sense, since whether or not c is declared would depend at runtime on whether b == 1. Documented? Where?

"if" and "else if" open new scopes. "static if" and "else static if" don't open new scopes. { // scope:1 if(a) // scope:1.2 ... else if(b) // scope:1.3 ... else static if(c) // scope:1 ... else // scope:1.4 ... } Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCsxAX3w+/yD4P9tIRAoa6AJ98andjl0cxgPqTGzSYYHEI0TpmhQCdFCB0 K+ZEm6zUKVvuEJ5/g6BdOXQ= =0x5q -----END PGP SIGNATURE-----
Jun 17 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Thomas Kuehne wrote:
<snip>
 "if" and "else if" open new scopes.
 "static if" and "else static if" don't open new scopes.
 
 { // scope:1
 	if(a)			// scope:1.2
 		...
 	else if(b)		// scope:1.3
 		...
 	else static if(c) 	// scope:1
 		...
 	else			// scope:1.4			
 		...
 }

Not quite: (a) the 'else' in 'else static if' above belongs to the non-static if above it, and so the 'else' itself opens a scope. (b) the final 'else' belongs to a 'static if', so it doesn't create a scope. So in fact, it's like this { // scope:1 if(a) // scope:1.1 ... else // scope:1.2 if(b) // scope:1.2.1 ... else // scope:1.2.2 static if(c) // scope:1.2.2 ... else // scope:1.2.2 ... } When you have 'else if', you can pretend that the scope just inside the 'else' doesn't exist, as long as you don't try to mix static and non-static ifs at the same indentation level. Thus { // scope:1 if(a) // scope:1.1 ... else if(b) // scope:1.2 ... else // scope:1.3 static if(c) // scope:1.3 ... else // scope:1.3 ... } Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 20 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 20 Jun 2005 10:28:07 +0100, Stewart Gordon wrote:

 Thomas Kuehne wrote:
 <snip>
 "if" and "else if" open new scopes.
 "static if" and "else static if" don't open new scopes.
 
 { // scope:1
 	if(a)			// scope:1.2
 		...
 	else if(b)		// scope:1.3
 		...
 	else static if(c) 	// scope:1
 		...
 	else			// scope:1.4			
 		...
 }

Not quite: (a) the 'else' in 'else static if' above belongs to the non-static if above it, and so the 'else' itself opens a scope. (b) the final 'else' belongs to a 'static if', so it doesn't create a scope. So in fact, it's like this { // scope:1 if(a) // scope:1.1 ... else // scope:1.2 if(b) // scope:1.2.1 ... else // scope:1.2.2 static if(c) // scope:1.2.2 ... else // scope:1.2.2 ... } When you have 'else if', you can pretend that the scope just inside the 'else' doesn't exist, as long as you don't try to mix static and non-static ifs at the same indentation level. Thus { // scope:1 if(a) // scope:1.1 ... else if(b) // scope:1.2 ... else // scope:1.3 static if(c) // scope:1.3 ... else // scope:1.3 ... } Stewart.

The more that I see 'static if', the more I think it is really dumb. Not the concept, which is just great, but the syntax. It is almost like it was purposefully designed to be as confusing and obscure as possible. I would argue that a *single* token for 'static if' and another for 'static else' would make it much easier to use. Some token names that indicate a compile-time action would be useful too. -- Derek Parnell Melbourne, Australia 20/06/2005 9:00:23 PM
Jun 20 2005
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
What about version else then?  Or, I guess, versionelse :P.

Personally, I like else just fine, and I think the syntax is very 
understandable.  I guess it's all about how you lay it out, and how you 
think about it.

To me, else means just that - it's not a part of if, to me, but 
rather... "else".

-[Unknown]


 On Mon, 20 Jun 2005 10:28:07 +0100, Stewart Gordon wrote:
 
 
Thomas Kuehne wrote:
<snip>

"if" and "else if" open new scopes.
"static if" and "else static if" don't open new scopes.

{ // scope:1
	if(a)			// scope:1.2
		...
	else if(b)		// scope:1.3
		...
	else static if(c) 	// scope:1
		...
	else			// scope:1.4			
		...
}

Not quite: (a) the 'else' in 'else static if' above belongs to the non-static if above it, and so the 'else' itself opens a scope. (b) the final 'else' belongs to a 'static if', so it doesn't create a scope. So in fact, it's like this { // scope:1 if(a) // scope:1.1 ... else // scope:1.2 if(b) // scope:1.2.1 ... else // scope:1.2.2 static if(c) // scope:1.2.2 ... else // scope:1.2.2 ... } When you have 'else if', you can pretend that the scope just inside the 'else' doesn't exist, as long as you don't try to mix static and non-static ifs at the same indentation level. Thus { // scope:1 if(a) // scope:1.1 ... else if(b) // scope:1.2 ... else // scope:1.3 static if(c) // scope:1.3 ... else // scope:1.3 ... } Stewart.

The more that I see 'static if', the more I think it is really dumb. Not the concept, which is just great, but the syntax. It is almost like it was purposefully designed to be as confusing and obscure as possible. I would argue that a *single* token for 'static if' and another for 'static else' would make it much easier to use. Some token names that indicate a compile-time action would be useful too.

Jun 20 2005
prev sibling parent clayasaurus <clayasaurus gmail.com> writes:
Apparently not, as you can see in d.D.announce under the dmd.126 
announcement, I was having trouble with 'else if' vs 'static else if', 
and you can see the difference with the sample code I attached (my last 
example, I posted the wrong one at first).


Unknown W. Brackets wrote:
 Isn't that the same as:
 
 void test()
 {
     const int a;
     int b;
 
     static if (a == 3)
     {
         // ...
     }
     else
     {
         if (b == 2)
         {
             // ...
         }
     }
 }
 
 Which is totally legal...?
 
 -[Unknown]
 
 
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 Is the following code legal, illegal or undefined?

 void test(){
     const int a;
     int b;
     static if(a==3){
         ...
     } else if(b==2){
         ...
     }
 }

 http://dstress.kuehne.cn/undefined/static_if_05_A.d
 http://dstress.kuehne.cn/undefined/static_if_05_B.d

 Thomas


 -----BEGIN PGP SIGNATURE-----

 iD8DBQFCqczs3w+/yD4P9tIRAo6kAJwOP8qYWovTFgJWhWLx+ZLlo5K4ygCgnplj
 vS8vTWaFfxXNFUKdTIkhJaY=
 =LdZW
 -----END PGP SIGNATURE-----


Jun 10 2005
prev sibling next sibling parent "Walter" <newshound digitalmars.com> writes:
No, there's no bug. It's well defined. static-if-else is a *completely
different* statement than if-else. It might make more sense to if you
replace "static if(a == 3)" with "version (identifier)", as version-else and
static-if-else are very analogous in their behavior.
Jun 10 2005
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Thomas Kuehne wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Is the following code legal, illegal or undefined?
 
 void test(){
 	const int a;

Good question. Should it be legal to declare a const without a value? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 14 2005
parent "Walter" <newshound digitalmars.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:d8mag8$p52$1 digitaldaemon.com...
 Thomas Kuehne wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 Is the following code legal, illegal or undefined?

 void test(){
 const int a;

Good question. Should it be legal to declare a const without a value?

Sure. It gets set to the default initializer for the type.
Jun 14 2005