Jump to page: 1 2
Thread overview
mixing static if and if
Jun 10, 2005
Thomas Kuehne
Jun 10, 2005
Thomas Kuehne
Jun 10, 2005
Thomas Kuehne
Jun 10, 2005
Thomas Kuehne
Jun 14, 2005
Stewart Gordon
Jun 17, 2005
Thomas Kuehne
Jun 20, 2005
Stewart Gordon
Jun 20, 2005
Derek Parnell
Jun 10, 2005
clayasaurus
Jun 11, 2005
Walter
Jun 14, 2005
Stewart Gordon
Jun 14, 2005
Walter
June 10, 2005
-----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-----
June 10, 2005
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-----
June 10, 2005
-----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-----
June 10, 2005
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-----
June 10, 2005
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-----
June 10, 2005
-----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)
> 			{
> 			}
> 		}
> 	}
	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.

Thomas


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

iD8DBQFCqgFm3w+/yD4P9tIRAg3lAKCJdhpKjjLELy2oCvmxcLtYymwDKQCgxZ9g
g2X6c3SvzLTvJXIvkAOMmF4=
=xXMq
-----END PGP SIGNATURE-----
June 10, 2005
-----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.

Thomas


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

iD8DBQFCqgan3w+/yD4P9tIRAqE1AJ9uEdyJ6mNaDVoATcQ/8Nx5hVlkSwCfRjl2
SYtOdJSv5ZQRpkxaVv1hWb4=
=4A/d
-----END PGP SIGNATURE-----
June 11, 2005
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.


June 14, 2005
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.
<snip>

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.
June 14, 2005
Thomas Kuehne wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Is the following code legal, illegal or undefined?
> 
> void test(){
> 	const int a;
<snip>

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.
« First   ‹ Prev
1 2