How to do foreach'able template Array?

Derek Parnell derek at nomail.afraid.org
Fri Sep 1 00:31:03 PDT 2006


On Fri, 01 Sep 2006 10:21:57 +0300, unknown wrote:

> How to implement foreachable template Array class:
> 
> class Array(T)
> {
>    T[] arr;
> 
>    public this() {}
>    public int count() { return arr.length; }
>    public void add(T t) { arr.length = arr.length+1; arr[length-1] = t; }
> 
>    opApply() {
>      ???
>    }
> }
> 
> I could not find out how to write correct operator opApply.

    int opApply(int delegate(inout T elem) dg)
    {   int result = 0;

	for (int i = 0; i < arr.length; i++)
	{
	    result = dg(arr[i]);
	    if (result)
		break;
	}
	return result;
    }
    int opApply(int delegate(inout int idx; inout T elem) dg)
    {   int result = 0;

	for (int i = 0; i < arr.length; i++)
	{
	    result = dg(i, arr[i]);
	    if (result)
		break;
	}
	return result;
    }
-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
1/09/2006 5:28:19 PM



More information about the Digitalmars-d-learn mailing list