alternate foreach semantics proposal

BCS ao at pathlink.com
Fri Apr 13 16:20:52 PDT 2007


When I am writing integrators, I keep finding my self writing function of 
this pattern:

|class C
|{
|  int delegate(int delegate(inout T)) Fn(Args args)
|  {
|    struct S
|    {
|      Args a;
|      int fn(int delegate(inout T) dg)
|      {
|        // loop using a for filtering/order/etc.
|          if(int ret = dg(i))
|            return ret;
|        return 0;
|      }
|    }
|    auto ret = new S;
|    ret.args = args
|    return &ret.fn;
|  }
|}

genealy it gets used like this

|C c = new C;
|foreach(T t; c.Fn(args)) {...}

This does a new and the almost first thing, drops the struct. It would be 
really nice if this could be rearranged like this:

|class C
|{
|  int Fn(int delegate(inout T), Args args)
|  {
|    // loop using args for filtering/order/etc.
|      if(int ret = dg(i))
|        return ret;
|    return 0;
|  }
|}

C c = new C;
foreach(T t; c.Fn(args)) {...}

The foreach would allow a function where the first parameter is a "int delegate(inout 
T)" to be used as an literator and have the other parameters passed from 
the "invocation".





More information about the Digitalmars-d mailing list