Few things II

BCS ao at pathlink.com
Thu Aug 9 08:38:08 PDT 2007


Reply to bearophile,

> --------------------------
> 
> BCS <ao at pathlink dot com>:
> 
>> under the covers, yeild is a disaster IMO<
>> 
> Maybe this is true for a low level language, I don't know. But in
> Python it's very useful and quite commonly used (but I don't like its
> V.2.5 use for coroutines, it's too much confusing and its syntax is
> dirty because it looks like a statement used as an expression). It's
> used in C# too and it seem to cause no disasters :-)

I am currently working on a project that is written in C# but which we are 
considering converting to D. The use of yield has turned out to be a significant 
performance problem. This is the reson I don't like it.

> It avoids you to
> mess with the opApply.
> 

D's opApply syntax IMO seems much cleaner and easier to understand. Also 
it can work without any heap allocations.


OTOH you can fake it quiet well with an delegate that stores most of it's 
state in an object.

|class Walk(T)
|{
|  T[] dat;
|  int at;
|
|  this(T[] d){dat = d; at = 0;}
|
|  int opApply(int delegate(inout int, inout T) dg)
|  {
|    while(at < dat.length)
|    {
|      int j = at;
|      if(int ret = dg(j, dat[at])) return ret; //yeild somtimes
|      at++;
|    }
|  }
|}
|
|auto walker = new Walk!(char)("hello world");
|foreach(inout int i, inout char c; walker) if(c == ' ') break;  //skip hello
|foreach(inout int i, inout char c; walker) writef("%s", c);     //prints 
world

> --------------------------





More information about the Digitalmars-d mailing list