view run/foreach_10.d @ 1330:f3f715978184

Georg Wrede <georg@iki.fi> 2007-01-07 mail:45A120F5.1050108@iki.fi
author thomask
date Sat, 13 Jan 2007 10:33:49 +0000
parents f87ba6507260
children 20d8ee6523e1
line wrap: on
line source

// $HeadURL$
// $Date$
// $Author$

module dstress.run.foreach_10;

class MyArray{
	double[] array;
	
	this(){
		array=new double[3];
		array[0]=0.0;
		array[1]=4.0;
		array[2]=8.0;
	}
	
	int opApply(int delegate(inout double) dg){
		double result;
		for(int i=0; i<array.length; i++){
			result = dg(array[i]);
			if(result){
				return i;
			}
		}
		return array.length;
	}
}
int main(){
	MyArray array=new MyArray();
	double test=0.0;
	assert(test==0.0);
	foreach(double value; array){
		test+=value;
	}
	assert(test==12.0);
	return 0;
}