Re: Little quiz

2011-03-28 Thread Kai Meyer

On 03/24/2011 06:50 PM, bearophile wrote:

A little quiz for people here: guess the output of this little D2 program (it 
compiles correctly and doesn't crash at run time, so it's a fair question):


import std.typecons: tuple;
import std.c.stdio: printf;

auto foo() {
 printf(foo\n);
 return tuple(1, 2);
}

void main() {
 foreach (x; foo().tupleof)
 printf(%d\n, x);
}


Bye,
bearophile


That's pretty cool :) Seems like we should be able to expect the same 
behavior in all of these, but that doesn't appear to be the case at all.




import std.typecons: tuple;
import std.c.stdio: printf;

auto foo() {
printf(foo\n);
return tuple(1, 2);
}

void main() {
printf(-\n);
foreach (x; foo().tupleof)
printf(%d\n, x);
printf(-\n);
auto f = foo();
printf(-\n);
foreach (x; f.tupleof)
printf(%d\n, x);
printf(-\n);
auto f2 = foo().tupleof;
printf(-\n);
foreach (x; f2)
printf(%d\n, x);
printf(-\n);
}



Re: Little quiz

2011-03-28 Thread bearophile
Kai Meyer:

  auto f2 = foo().tupleof;

Thank you. From the output of this line of code the problem seems not caused by 
the static foreach.

Bye,
bearophile


Little quiz

2011-03-24 Thread bearophile
A little quiz for people here: guess the output of this little D2 program (it 
compiles correctly and doesn't crash at run time, so it's a fair question):


import std.typecons: tuple;
import std.c.stdio: printf;

auto foo() {
printf(foo\n);
return tuple(1, 2);
}

void main() {
foreach (x; foo().tupleof)
printf(%d\n, x);
}


Bye,
bearophile


Re: Little quiz

2011-03-24 Thread spir

On 03/25/2011 01:50 AM, bearophile wrote:

A little quiz for people here: guess the output of this little D2 program (it 
compiles correctly and doesn't crash at run time, so it's a fair question):


import std.typecons: tuple;
import std.c.stdio: printf;

auto foo() {
 printf(foo\n);
 return tuple(1, 2);
}

void main() {
 foreach (x; foo().tupleof)
 printf(%d\n, x);
}


lol, would never haver guessed

Denis
--
_
vita es estrany
spir.wikidot.com



Re: Little quiz

2011-03-24 Thread Jesse Phillips
bearophile Wrote:

 A little quiz for people here: guess the output of this little D2 program (it 
 compiles correctly and doesn't crash at run time, so it's a fair question):
 
 
 import std.typecons: tuple;
 import std.c.stdio: printf;
 
 auto foo() {
 printf(foo\n);
 return tuple(1, 2);
 }
 
 void main() {
 foreach (x; foo().tupleof)
 printf(%d\n, x);
 }

Starting from main. As we are performing a foreach over a tuple this will need 
to happen at compilation. As their are many bugs with compile time foreach I 
would think this code evaluates to nothing and thus the program prints nothing.

However if that is working then I would expect foo() to be executed at 
compile-time which would mean 'foo' might be printed during compilation. As 
tupleof is supposed to return a type tuple, I'm unsure what gibberish printing 
it as a decimal would do.

The other likely possibility is that the foreach is unrolled into code like:

x = foo().tupleof[0]
print...
x = foo().tupleof[1]
print...

where .tupleof is some other fancy runtime thing. In this case you would get 
foo\nnumber\nfoo\nnumber...

--

So yeah, from that code I have no idea what it is supposed to do. But I am not 
surprised by its behavior.