www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Little quiz

reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 24 2011
next sibling parent spir <denis.spir gmail.com> writes:
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
Mar 24 2011
prev sibling next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Mar 24 2011
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-03-25 01:50, 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
I would guess it prints the values of all the fields in the struct returned by "tuple". -- /Jacob Carlborg
Mar 25 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jacob Carlborg:

 I would guess it prints the values of all the fields in the struct 
 returned by "tuple".
That was of course my purpose, because tuples are a way to implement multiple return values, and in some situations I want to print all the items of such return tuple, on separated lines, for debugging purposes, etc. But unfortunately (for me too) that's not the right answer. Try again... I have found a little surprise. Bye, bearophile
Mar 25 2011
parent Jacob Carlborg <doob me.com> writes:
On 2011-03-25 12:32, bearophile wrote:
 Jacob Carlborg:

 I would guess it prints the values of all the fields in the struct
 returned by "tuple".
That was of course my purpose, because tuples are a way to implement multiple return values, and in some situations I want to print all the items of such return tuple, on separated lines, for debugging purposes, etc. But unfortunately (for me too) that's not the right answer. Try again... I have found a little surprise. Bye, bearophile
I'm not just meaning the expected values you pass in to the function, I'm also referring to any additional fields that might be present in the struct. -- /Jacob Carlborg
Mar 25 2011
prev sibling next sibling parent Kagamin <spam here.lot> writes:
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
According to docs tupleof returns type tuple, but TypeTuple doesn't seem to be iteratable.
Mar 25 2011
prev sibling parent reply Kai Meyer <kai unixlords.com> writes:
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"); }
Mar 28 2011
parent bearophile <bearophileHUGS lycps.com> writes:
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
Mar 28 2011