popFront with input variables

2012-08-31 Thread Joseph Rushton Wakeling

Hello all,

Is it considered legit in any circumstances for popFront to take an input 
variable (e.g. a random number generator)?  Or is it required always to have no 
input variables?


Thanks & best wishes,

-- Joe


Re: popFront with input variables

2012-08-31 Thread bearophile

Joseph Rushton Wakeling:

Is it considered legit in any circumstances for popFront to 
take an input variable (e.g. a random number generator)?  Or is 
it required always to have no input variables?


popFront is meant to be called by foreach, or to be verified by
the isSomething compile-time tests of std.range. In both cases if
popFront takes an argument, it doesn't work. So I think it's not
a good idea.

struct Foo {
 enum bool empty = false;
 @property int front() { return 1; }
 void popFront(int x) {}
 //void popFront() {}
}
void main() {
 import std.stdio;
 foreach (i; Foo())
 writeln(i);
}


temp.d(9): Error: function temp.Foo.popFront (int x) is not
callable using argument types ()
temp.d(9): Error: expected 1 function arguments, not 0

Bye,
bearophile


Re: popFront with input variables

2012-08-31 Thread Simen Kjaeraas
On Fri, 31 Aug 2012 16:56:32 +0200, Joseph Rushton Wakeling  
 wrote:



Hello all,

Is it considered legit in any circumstances for popFront to take an  
input variable (e.g. a random number generator)?  Or is it required  
always to have no input variables?


No parameters, or at least it should be callable with no parameters.

Might I ask why you'd want this?

--
Simen


Re: popFront with input variables

2012-08-31 Thread Jonathan M Davis
On Friday, August 31, 2012 15:56:32 Joseph Rushton Wakeling wrote:
> Hello all,
> 
> Is it considered legit in any circumstances for popFront to take an input
> variable (e.g. a random number generator)? Or is it required always to have
> no input variables?

Don't do it. Technically speaking, as long as it's callable with no arguments, 
you should be able to do it. isInputRange just checks that it's callable with 
no arguments:

template isInputRange(R)
{
 enum bool isInputRange = is(typeof(
 (inout int _dummy=0)
 {
 R r = void; // can define a range object
 if (r.empty) {} // can test for empty
 r.popFront(); // can invoke popFront()
 auto h = r.front; // can get the front of the range
 }));
}

but no range-based anything is going to call popFront with any arguments, and 
it goes against how popFront works. If you want a function which takes an 
argument and pops of the front as well, then create a new one. Don't use 
popFront for that. I have no idea why you'd ever _want_ to do that though. You 
have a highly abnormal use case if it makes any sense for you to be declaring 
a popFront which takes an argument.

- Jonathan M Davis