Little quiz
Kai Meyer
kai at unixlords.com
Mon Mar 28 07:55:26 PDT 2011
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");
}
More information about the Digitalmars-d-learn
mailing list