Custom Blocks

Chris Williams aahz at seanet.com
Wed Aug 11 12:40:40 PDT 2010


== Quote from Nick Sabalausky (a at a.a)'s article
> "Chris Williams" <aahz at seanet.com> wrote in message
> > That brings us to the second problem. Our last parameter for a
custom
> > block is always "void delegate()" and our return is always void.
> Currently, all blocks are void, but I'm not sure that limitation
should be
> preserved for custom blocks. Example:
> auto result = collection.reduce(a, b) { return a + b; };

I considered whether there might be some value in having a block that
returns a value and couldn't think of anything that wasn't better done
by a true function. A one-use callback for something like QSort or
whatever, makes sense though, I'd agree. But, if implemented, I'd
wonder if a secondary keyword wouldn't be better like "break" instead
of "return", so that there was no confusion of what scope was being
referred to -- and so that one -could- actually return from the higher
scope, like a regular local block.

void doStuff() {
   lock(o) {
      if (o.x > 10) {
         break; // Leaves lock()
      }
      else {
         return; // Leaves doStuff()
      }
   }

   int res = collection.reduce(a, b) {
      if (a > 0 && b > 0) {
         break a + b; // Leaves reduce()
      }
      else {
         return; // Leaves doStuff()
      }
   }
}

That is sort of ugly, though.


More information about the Digitalmars-d mailing list