Groovy
David Medlock
noone at nowhere.com
Tue Jan 2 06:29:46 PST 2007
Jarrett Billingsley wrote:
> "bls" <killing__Zoe at web.de> wrote in message
> news:enb0r2$51f$1 at digitaldaemon.com...
>
>
>>Suneido supports Smalltalk style "blocks". Basically, a block is a section
>>of code within a function, that can be called like a function, but that
>>operates within the context of the function call that created it (i.e.
>>shares its local variables).
>
>
> Nested functions and delegate literals in D do (almost, see below) the same
> thing. Access to outer locals is a very useful feature indeed.
>
>
>>Blocks can be used to implement user defined "control constructs". (In
>>Smalltalk, all control constructs are implemented with blocks.) For
>>example,
>>you could implement your own version of "foreach":
>>
>>for_each = function (list, block)
>>{
>>for (i = 0; i < list.Size(); ++i)
>>block(list[i])
>>}
>>list = #(12, 34, 56)
>>for_each(list)
>>{ |x| Print(x) }
>>=> 12
>>34
>>56
>
>
> What's funny about this example is that this is _precisely_ how D handles
> iteration with foreach, except that it's hidden behind loop-like syntax.
> The body of a foreach loop is converted into a nested function and passed as
> a callback to the opApply for the given container.
>
>
>>Suneido treats a block immediately following a function call as an
>>additional argument.
>
>
> This is a really cool idea that some people (myself included) would like to
> see in D. The closest thing we have now is:
>
> for_each(list, (x)
> {
> writefln(x);
> });
>
>
>>Blocks can also be used to execute sections of code in specific
>>"contexts".
>>For example, the Catch function traps exceptions and returns them. (This
>>is
>>useful in unit tests to verify that expected exceptions occur.)
>>
>>catcher = function (block)
>>{
>>try
>>return block()
>>catch (x)
>>return x
>>}
>>catcher( { xyz } ) => "unitialized variable: xyz"
>
>
Hehe.
Deja Vu Jarrett(times 2 I think):
http://www.digitalmars.com/d/archives/digitalmars/D/24770.html
I do think that lazy expressions get us 80 or 90 percent of the way
there in most cases, though.
-DavidM
More information about the Digitalmars-d
mailing list