www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.stdio.writeln should not accept infinite ranges?

reply Xinok <xinok live.com> writes:
The following code compiles and runs:

import std.stdio, std.random;

void main()
{
     writeln(rndGen);
}

Since rndGen is an infinite range, this code runs forever. It 
seems to be that we need to add a check for isInfinite on writeln 
and other related functions.

Does anybody have a use case where this is actually practical? I 
don't see a reason for allowing infinite ranges here, considering 
how easy it is to write "range.take(n)".
Nov 12 2015
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/12/2015 08:18 PM, Xinok wrote:
 The following code compiles and runs:

 import std.stdio, std.random;

 void main()
 {
      writeln(rndGen);
 }

 Since rndGen is an infinite range, this code runs forever. It seems to
 be that we need to add a check for isInfinite on writeln and other
 related functions.

 Does anybody have a use case where this is actually practical? I don't
 see a reason for allowing infinite ranges here, considering how easy it
 is to write "range.take(n)".
Piping a program that produces infinite output into less is practical. -- Andrei
Nov 12 2015
parent reply Xinok <xinok live.com> writes:
On Friday, 13 November 2015 at 01:19:22 UTC, Andrei Alexandrescu 
wrote:
 On 11/12/2015 08:18 PM, Xinok wrote:
 The following code compiles and runs:

 import std.stdio, std.random;

 void main()
 {
      writeln(rndGen);
 }

 Since rndGen is an infinite range, this code runs forever. It 
 seems to
 be that we need to add a check for isInfinite on writeln and 
 other
 related functions.

 Does anybody have a use case where this is actually practical? 
 I don't
 see a reason for allowing infinite ranges here, considering 
 how easy it
 is to write "range.take(n)".
Piping a program that produces infinite output into less is practical. -- Andrei
Fair enough. I'm thinking more on the side of safety though and I assume that, more often than not, printing an infinite range is unintentional. Should it be this easy to shoot ourselves in the foot? I'm not saying it should be impossible, but just make it explicit that we intended to print an infinite range.
Nov 12 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/12/15 10:21 PM, Xinok wrote:
 On Friday, 13 November 2015 at 01:19:22 UTC, Andrei Alexandrescu wrote:
 On 11/12/2015 08:18 PM, Xinok wrote:
 The following code compiles and runs:

 import std.stdio, std.random;

 void main()
 {
      writeln(rndGen);
 }

 Since rndGen is an infinite range, this code runs forever. It seems to
 be that we need to add a check for isInfinite on writeln and other
 related functions.

 Does anybody have a use case where this is actually practical? I don't
 see a reason for allowing infinite ranges here, considering how easy it
 is to write "range.take(n)".
Piping a program that produces infinite output into less is practical. -- Andrei
Fair enough. I'm thinking more on the side of safety though and I assume that, more often than not, printing an infinite range is unintentional. Should it be this easy to shoot ourselves in the foot? I'm not saying it should be impossible, but just make it explicit that we intended to print an infinite range.
I'm with Andrei on this. It's easy to create infinite loops, this is a pitfall of programming. Luckily, your foot will heal from this for next time. -Steve
Nov 13 2015