Template accepting code blocks as a parameter.
Andrew Fedoniouk
news at terrainformatica.com
Wed Mar 1 10:53:34 PST 2006
"David Medlock" <noone at nowhere.com> wrote in message
news:du4dsl$pme$1 at digitaldaemon.com...
>> Let's say I'll be able to define mixin template which
>> is able to accept block of code (aka closure) as a parameter.
>>
>> So if I declare something like this:
>>
>> template on_scope_exit(B: void delegate() )
>> {
>> auto ScopeGuard sc#__LINE__ = new ScopeGuard(B);
>> }
>>
>> then I would like to be able to write something like this
>>
>> mixin on_scope_exit! { delete foo; }
>>
>> Which will expand to something like this at the point of mixin:
>>
>> auto ScopeGuard sc1234 = new ScopeGuard( { delete foo; } );
>>
>> This is just a rough idea.
>>
>> Having this will allow to define onScope*** in the way that task needs
>> and I beleive it will be useful in other places too.
>>
> I suggested this some time ago:
>
> http://www.digitalmars.com/d/archives/digitalmars/D/24770.html
>
> If implemented it would allow foreach() to be implemented as a template.
>
Yep, I was thinking about foreach too.
Now foreach is a bit limited - no filtering, direction, etc.
templates with closure (auto-delegates) parameters is a
promising direction to be short.
In fact proposed scope guards on_scope_... are just another forms of
mixins :
mixin onfailure { ... }
mixin onsuccess { ... }
another form:
mixin onfailure myOnFailure!(parameters);
mixin onsuccess myOnSuccess!(parameters);
In reality they are mixing code blocks at correspondent exits of scope
execution.
I beleive that design of such mixins is more generic. Sure these
on_scope_*** solve
some aesthetical problems. But having such on*** mixins and delegate-mixins
will open way more possibilities.
Andrew.
http://terrainformatica.com
More information about the Digitalmars-d
mailing list