www.digitalmars.com         C & C++   DMDScript  

D - temlates, toApply question?

D templates are realy OK but they are very badly documented and it is realy
hard for
a D-beginner to figure out how the work! There should be a couple of
meaningful examples
in the documentation!

I have a problem understanding a  part of code i have writen.
It is set class template and it basicaly looks like this:

class set(SetType)
{
     public this() {}
     public this(in set a)
     {
          foreach(SetType elem; a)
          {
               this.insert(elem);
          }
     }
     public this(in SetType[] array)
     {
          for(int i=0; i<array.length; i++)
          this.insert(array[i]);
     }
     public bool insert(in SetType a)
     {
          if(!(a in val)) {val[a]=1;}
          return true;
     }

//...some operators for comarison, union, intersction...

    private int[SetType] val;

//i figured out that i neaded to write opApply to be able to iterate class
set with foreach

     int opApply(int delegate(inout SetType) dg)
    {
          int result;
          foreach(SetType st; skup.keys)
          {
               result = dg(st);
               if(result)
                break;
          }
        return result;
    }
}

the opApply function i wrote works good but i wrote it by trying for some
time
and i don't understand what should this function do, what does delegate dg
do,
and how does foreach use this opApply function?

One more question: how could i add to the class a member template function
that would receive a set class instance instantiated with another type?
I tried this in my class:

template Func(T)
{
    static void Func(set!(T) a) {}
}

but it doesn work?

Thanks for answering my questions!:)
Feb 04 2004