Proposal: user defined attributes
F i L
witte2008 at gmail.com
Tue Mar 20 02:49:33 PDT 2012
Jacob Carlborg wrote:
> If D supported passing delegates to a function after the
> function call several language features could have been
> implemented in the library instead.
>
> void synchronized (void delegate () dg)
> {
> try
> {
> lock();
> dg();
> }
> finally
> unlock();
> }
>
> synchronized {
> // do something
> }
>
> In the same way foreach can be implemented in a library
> function:
>
> void foreach (T) (T[] arr, void delegate (T) dg)
> {
> for (size_t i = 0; i < arr.length; i++)
> dg(arr[i]);
> }
>
> auto arr = [3, 4, 5];
>
> foreach (arr ; e // this is the parameter to the delegate) {
> writeln(e);
> }
That's pretty slick. D would need delegate inlining +
@forceInline before foreach could be expressed this way though.
More information about the Digitalmars-d
mailing list