July 08, 2013
On Monday, 8 July 2013 at 12:41:22 UTC, John Colvin wrote:
> The i++ in to!string not having any effect is rather surprising, I agree.

Scratch that, there's nothing surprising about it at all. i == 0 on the first iteration and therefore the first branch of the ternary isn't executed. The same thing happens the next iteration and so on, leaving i==0 for every iteration.
July 08, 2013
On Monday, 8 July 2013 at 12:11:36 UTC, Dicebot wrote:
> On Monday, 8 July 2013 at 12:08:44 UTC, JS wrote:
>> the issue with the foreach is still under question though, when I try to use a for loop with i and access TT[i] I get an error about i not being compile time readable. (maybe this is because of how the type tuple is defined? possibly need a index method to get the value?)
>
> Because i is not a compile-time value, it is a normal variable. And TT is a type tuple, pure compile-time entity. You really need to study the documentation on this topic or frustration will continue.

This has nothing to do with it...

string name = "Value"~((i==0) ? "" : (to!string(i)));
i++;

works, but

string name = "Value"~((i==0) ? "" : (to!string(i++)));

but doesn't....


It is a matter of placement of the increment operation on i that is breaking the code and has nothing to do with your default answer of "you don't know how CTFE's work, read the docs!" and "... it's not a compile-time variable".

If i's not possible to use at compile time then none of the following code examples should work

One could argue that depending on how to!string works could be the issue, but this has nothing to do with i, which is what you have stated, but to!string.

If it is to!string or templates in general that are the issue then it is confusing to have the same syntax for compile time and runtime but some common runtime syntax doesn't work.

e.g.,

	int x = 0;
	writeln(x);
	to!string(x++);
	writeln(x);

works fine at run-time, but to!string(i++) fails and to!string(i); i++; works in CTFE...

So, either this is what you were getting at but didn't explain it well(making the issue about i when it is about CTFE templates) or you didn't understand the issue yourself.


	int i = 0;
	foreach(t; T)
	{
		string name = "Value"~((i==0) ? "" : (to!string(i)));
		i++;
	}



vs

	int i = 0;
	foreach(t; T)
	{
		string name = "Value"~((i==0) ? "" : (to!string(i++)));		
	}

vs


	foreach(i, t; T)
	{
		string name = "Value"~((i==0) ? "" : (to!string(i)));
	}


all are suppose to do the same thing and are all essentially semantically identical... they should do the same. (at least according to your logic and the compiler error message that says the issue is with i)

July 08, 2013
Ultimately, the point is, that I thought CTFE's were suppose to be compile time runnable functions. The problem is, the actual language grammar changes. can use i++ as an argument to a template at runtime without issue but not at compile time... and since this seems to be the case from the examples I've posted, this causes, at least for me, a lot of confusion. (who knows what other differences there are) (and IMO they are flaws in the CTFE system... although maybe there is some deep underlying reason why it must be done that way)

July 08, 2013
On 07/09/13 00:57, JS wrote:
> On Monday, 8 July 2013 at 12:11:36 UTC, Dicebot wrote:
>> On Monday, 8 July 2013 at 12:08:44 UTC, JS wrote:
>>> the issue with the foreach is still under question though, when I try to use a for loop with i and access TT[i] I get an error about i not being compile time readable. (maybe this is because of how the type tuple is defined? possibly need a index method to get the value?)
>>
>> Because i is not a compile-time value, it is a normal variable. And TT is a type tuple, pure compile-time entity. You really need to study the documentation on this topic or frustration will continue.
> 
> This has nothing to do with it...
> 
> string name = "Value"~((i==0) ? "" : (to!string(i)));
> i++;
> 
> works, but
> 
> string name = "Value"~((i==0) ? "" : (to!string(i++)));
> 
> but doesn't....
> 
> 
> It is a matter of placement of the increment operation on i that is breaking the code and has nothing to do with your default answer of "you don't know how CTFE's work, read the docs!" and "... it's not a compile-time variable".

What should the following program print?

   void main() {
      import std.stdio;
      auto a = 0;
      a==0 ? writeln("Hello") : writeln("world!");
   }

artur
July 08, 2013
On Monday, 8 July 2013 at 22:57:17 UTC, JS wrote:
> 	int i = 0;
> 	foreach(t; T)
> 	{
> 		string name = "Value"~((i==0) ? "" : (to!string(i)));
> 		i++;
> 	}

That increments i, both ctfe and rt.
>
>
> vs
>
> 	int i = 0;
> 	foreach(t; T)
> 	{
> 		string name = "Value"~((i==0) ? "" : (to!string(i++)));		
> 	}

This will never increment i, whether in ctfe or at runtime. The entire "(to!string(i++))"  will never be executed.

> vs
>
>
> 	foreach(i, t; T)
> 	{
> 		string name = "Value"~((i==0) ? "" : (to!string(i)));
> 	}

This will increment i, both in ctfe and at runtime.
July 09, 2013
On Monday, 8 July 2013 at 23:36:46 UTC, John Colvin wrote:
> On Monday, 8 July 2013 at 22:57:17 UTC, JS wrote:
>> 	int i = 0;
>> 	foreach(t; T)
>> 	{
>> 		string name = "Value"~((i==0) ? "" : (to!string(i)));
>> 		i++;
>> 	}
>
> That increments i, both ctfe and rt.
>>
>>
>> vs
>>
>> 	int i = 0;
>> 	foreach(t; T)
>> 	{
>> 		string name = "Value"~((i==0) ? "" : (to!string(i++)));		
>> 	}
>
> This will never increment i, whether in ctfe or at runtime. The entire "(to!string(i++))"  will never be executed.

oh shit! lol... fuck I'm stupid! ;/

>> vs
>>
>>
>> 	foreach(i, t; T)
>> 	{
>> 		string name = "Value"~((i==0) ? "" : (to!string(i)));
>> 	}
>
> This will increment i, both in ctfe and at runtime.


July 09, 2013
Am Tue, 09 Jul 2013 01:02:12 +0200
schrieb "JS" <js.mdnq@gmail.com>:

> Ultimately, the point is, that I thought CTFE's were suppose to be compile time runnable functions. The problem is, the actual language grammar changes. can use i++ as an argument to a template at runtime without issue but not at compile time... and since this seems to be the case from the examples I've posted, this causes, at least for me, a lot of confusion. (who knows what other differences there are) (and IMO they are flaws in the CTFE system... although maybe there is some deep underlying reason why it must be done that way)

If you look through the thread, others said that the behavoir is the same in C at runtime. Your use of the ternary operator is the problem. Only the branch that is taken is executed. This should be obvious, but I agree that when the other branch contains an i++ it looks like it should be executed. Please look at the ternary operator as an if and else and it will become clear why your increment works when you put it on a separate line.

TL;DR: It has nothing to do with flaws in D or CTFE. At least not this time around. ;)

-- 
Marco

1 2
Next ›   Last »