Range returning an array

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 9 14:02:06 PDT 2013


On Tue, 09 Apr 2013 16:03:01 -0400, Joseph Rushton Wakeling  
<joseph.wakeling at webdrake.net> wrote:


> Any thoughts? :-)

1. documentation.  Make sure the user of the range knows that this data is  
going to be re-used.
2. Make front() return const if possible.  It is another signal that you  
aren't supposed to keep this data.
3. For your specific situation, add lastFront():

struct MySimulation(T)
{
	T[] var;
	T[] lastVar;
	T diff, convergence;

	auto front() @property
	{
		return var;
	}

	bool empty() @property
	{
		return (diff < convergence);
	}

	void popFront()
	{
		lastVar[] = var[];
		// update values in var
		// and calculate diff
	}
	
	auto lastFront() @property
	{
		return lastVar;
	}
}

-Steve


More information about the Digitalmars-d-learn mailing list