March 11, 2013
On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
> On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>> Actually, in D, static if creates its own scopes for declarations made
>> inside the static if condition.
>
> No, it doesn't,

Yes it does.

> and it would be _way_ less useful if it did,

I don't think so.

> particularly with
> regards to struct and class definitions. Take this code, for instance,
>
> import std.stdio;
>
> static if(true)
>      int var = 7;
> else
>      string var = 12;
>
> void main()
> {
>      int i = var;
>
>      static if(is(typeof(var) == int))
>          int j = 22;
>      else
>          float j;
>
>      writeln(j);
> }
>
> It compiles just fine and prints 22. Both the static if at module-level and the
> one in the function declare variables which are used outside of the static if
> blocks. static if does _not_ create a new scope. And putting braces around the
> static if bodies has no effect on the scoping either.
>
> ...

What is the point? Your example code does not make any declaration inside the static if condition.

March 11, 2013
On Mon, Mar 11, 2013 at 10:31:53PM +0100, Timon Gehr wrote:
> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
> >On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
> >>Actually, in D, static if creates its own scopes for declarations made inside the static if condition.
> >
> >No, it doesn't,
> 
> Yes it does.

Actually, it doesn't, scarily enough:

	import std.stdio;
	void main() {
		int[string] x;
		//float x;
		static if (is(typeof(x) S : T[U], T, U)) {
			writeln(S.stringof);
			writeln(T.stringof);
			writeln(U.stringof);
		}
		writeln(S.stringof); // <-- this compiles, and works!!
	}

The last writeln will fail to compile if x's type is changed to float
(as in the commented out line).

Meaning that the definitions of S, T, U "leak" past the scope of the static if. Which makes the semantics of the code very unclear, because whether or not it even compiles depends on how the static if condition turns out. :-/


T

-- 
Trying to define yourself is like trying to bite your own teeth. -- Alan Watts
March 11, 2013
On Monday, 11 March 2013 at 21:31:53 UTC, Timon Gehr wrote:
> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>> On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>>> Actually, in D, static if creates its own scopes for declarations made
>>> inside the static if condition.
>>
>> No, it doesn't,
>
> Yes it does.

No... It doesn't. He just showed you.

>> and it would be _way_ less useful if it did,
>
> I don't think so.

Half the code in phobos would be broken.

>> particularly with
>> regards to struct and class definitions. Take this code, for instance,
>>
>> import std.stdio;
>>
>> static if(true)
>>     int var = 7;
>> else
>>     string var = 12;
>>
>> void main()
>> {
>>     int i = var;
>>
>>     static if(is(typeof(var) == int))
>>         int j = 22;
>>     else
>>         float j;
>>
>>     writeln(j);
>> }
>>
>> It compiles just fine and prints 22. Both the static if at module-level and the
>> one in the function declare variables which are used outside of the static if
>> blocks. static if does _not_ create a new scope. And putting braces around the
>> static if bodies has no effect on the scoping either.
>>
>> ...
>
> What is the point? Your example code does not make any declaration inside the static if condition.

What *are* you talking about??? I can count 4 declarations in 2 static ifs? What is your definition of "declaration" and "scope".

There's a misunderstanding somewhere here. Can YOU show us an example where there is a declaration that is scoped?
March 11, 2013
On Monday, 11 March 2013 at 22:12:46 UTC, monarch_dodra wrote:
> On Monday, 11 March 2013 at 21:31:53 UTC, Timon Gehr wrote:
>> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>>> On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>>>> Actually, in D, static if creates its own scopes for declarations made
>>>> inside the static if condition.
>>>
>>> No, it doesn't,
>>
>> Yes it does.
>
> No... It doesn't. He just showed you.
>
> [...]
>
> There's a misunderstanding somewhere here. Can YOU show us an example where there is a declaration that is scoped?

OOhhhh!!! "Condition" Right.
March 11, 2013
On Monday, 11 March 2013 at 22:08:10 UTC, H. S. Teoh wrote:
> On Mon, Mar 11, 2013 at 10:31:53PM +0100, Timon Gehr wrote:
>> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>> >On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>> >>Actually, in D, static if creates its own scopes for declarations
>> >>made inside the static if condition.
>> >
>> >No, it doesn't,
>> 
>> Yes it does.
>
> Actually, it doesn't, scarily enough:
>
> 	import std.stdio;
> 	void main() {
> 		int[string] x;
> 		//float x;
> 		static if (is(typeof(x) S : T[U], T, U)) {
> 			writeln(S.stringof);
> 			writeln(T.stringof);
> 			writeln(U.stringof);
> 		}
> 		writeln(S.stringof); // <-- this compiles, and works!!
> 	}
>
> The last writeln will fail to compile if x's type is changed to float
> (as in the commented out line).
>
> Meaning that the definitions of S, T, U "leak" past the scope of the
> static if. Which makes the semantics of the code very unclear, because
> whether or not it even compiles depends on how the static if condition
> turns out. :-/
>
>
> T

I think that's bug material. The "is" definition should end at the end of that block.
March 11, 2013
On Saturday, 9 March 2013 at 00:48:59 UTC, DypthroposTheImposter
wrote:
>        See the static_if paper here:
>
> http://isocpp.org/forums
>
> Under the post "constraints and static if" there is a link to a
> document about
> static_if
>
> https://docs.google.com/viewer?a=v&pid=forums&srcid=MDIyMDc3NjUwMTczOTM0Mjk3NjABMDI2MzM3MjkxNDM4NDQ5MzE4NDcBLWVsS1Y4dFhtdDhKATUBaXNvY3BwLm9yZwF2Mg
>
>       Are they full of it? Has it caused the problems they mention
> in
> D?

What an unfair article. I guess someone is angry at D.

I don't think C++11 is that good an upgrade. Concepts were
delayed. Lambdas can't be templated, can't be recursive.
constexpr is useless compared to CTFE. Move semantics are easy to
use but much harder to really understand.
I recently had to duplicate inline assembly code in C++. I would
not have been forced to do it in D since (naked inline assembly +
static if) can save a lot of lines. static if allow to get
polymorphism at finer granularities.
March 11, 2013
On Monday, 11 March 2013 at 21:31:53 UTC, Timon Gehr wrote:
> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>> On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>>> Actually, in D, static if creates its own scopes for declarations made
>>> inside the static if condition.
>>
>> No, it doesn't,
>
> Yes it does.

No, it doesn't.

>> and it would be _way_ less useful if it did,
>
> I don't think so.

 Oh? Examples inside the TDPL would break as the inner scopes would be hidden and thereby inaccessible.

 TDPL pg. 279

[quote]
[code]
  template Select(bool cond, T1, T2) {
    static if (cond) {
      alias T1 Type;
    } else {
      alias T2 Type;
    }
  }

  unittest {
    alias Select!(false, int, string).Type MyType;
    static assert(is(MyType == string));
  }
[/code]
[/quote]

 Plus it's part of the spec.

 TDPL pg. 69
[quote]
Peeling Braces: There's a glaring oddity about the transmogrify example. See, the numeric type is introduced inside a pair of { and } braces. As such, it should be visible only locally inside that scope (and consequently invisible to the enclosing function), thus foiling our entire plan quite thoroughly. Such behavior would also render the promised static if statement practically useless. For that reason, the static if uses braces for [i]grouping[/i] but not for [i]scoping[/i]. As far as scope and visibility are concerned, static if peels the outermost braces away, if any (they are not required when you have only one controlled statement; out example above uses them only out of stylistic obsession). If you do want braces, just add another pair:

[code]
  void main(){
    static if (real.sizeof > double.sizeof) {{
      auto maximorum = real.max;
      writefln("Really big numbers - up to %s!", maximorum);
    }}
    ... /* maximorum is invisible here */ ...
  }
[/code]
[/quote]
March 11, 2013
On Monday, 11 March 2013 at 21:31:53 UTC, Timon Gehr wrote:
>>     static if(is(typeof(var) == int))
>>         int j = 22;
>
> What is the point? Your example code does not make any declaration inside the static if condition.

I'm not sure what your definition of declaration is, but that looks like one to me, with instantiation of course.
March 12, 2013
On 03/11/2013 11:06 PM, H. S. Teoh wrote:
> On Mon, Mar 11, 2013 at 10:31:53PM +0100, Timon Gehr wrote:
>> On 03/11/2013 09:19 PM, Jonathan M Davis wrote:
>>> On Monday, March 11, 2013 20:14:07 Timon Gehr wrote:
>>>> Actually, in D, static if creates its own scopes for declarations
>>>> made inside the static if condition.
>>>
>>> No, it doesn't,
>>
>> Yes it does.
>
> Actually, it doesn't, scarily enough:
>
> 	import std.stdio;
> 	void main() {
> 		int[string] x;
> 		//float x;
> 		static if (is(typeof(x) S : T[U], T, U)) {
> 			writeln(S.stringof);
> 			writeln(T.stringof);
> 			writeln(U.stringof);
> 		}
> 		writeln(S.stringof); // <-- this compiles, and works!!
> 	}
>
> The last writeln will fail to compile if x's type is changed to float
> (as in the commented out line).
>
> Meaning that the definitions of S, T, U "leak" past the scope of the
> static if. Which makes the semantics of the code very unclear, because
> whether or not it even compiles depends on how the static if condition
> turns out. :-/
>
>
> T
>

Wtf. :)

void main() {
    int[string] x;
    static if (is(typeof(x) S : T[U], T, U)) { }
    pragma(msg, S, " ", T, " ", U);
}

I have checked again and the documentation actually does not specify this. It just contains suggestive examples.
March 12, 2013
On 03/11/2013 11:15 PM, monarch_dodra wrote:
> ...
>
> I think that's bug material. The "is" definition should end at the end
> of that block.

http://d.puremagic.com/issues/show_bug.cgi?id=9693