www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template accepting code blocks as a parameter.

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
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() )
{

}

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.

Andrew
http://terrainformatica.com
Feb 28 2006
parent reply David Medlock <noone nowhere.com> writes:
Andrew Fedoniouk wrote:
 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() )
 {

 }
 
 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.
 
 Andrew
 http://terrainformatica.com
 
 
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. -DavidM
Mar 01 2006
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
"David Medlock" <noone nowhere.com> wrote in message 
news:du4dsl$pme$1 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() )
 {

 }

 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
Mar 01 2006