Custom Blocks

cemiller chris at dprogramming.com
Tue Aug 10 12:08:21 PDT 2010


On Mon, 09 Aug 2010 20:21:25 -0700, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> Chris Williams wrote:
>> I'm not sure whether the design of D 2.0 has stabilized as yet, but if  
>> not,
>> I would like to suggest the ability to create custom block types.
> [snip]
>
> FWIW we've been talking a long time ago about a simple lowering - if the  
> last argument to a function is a delegate, allow moving the delegate's  
> body outside of the function:
>
> fun(a, b, c) { body }
>
>   |
>   V
>
> fun((a, b, c) { body });
>
> As far as Walter and I could tell, there are no syntactical issues  
> created by such a lowering. But we've been wrong about that in the past  
> (me 10x more often than him).
>
>
> Andrei

I think this way will be better:
    fun(a, b, c) { body }
      |
      V
    fun(a, b, c, { body });

so that you can pass parameters to fun itself. If you want parameters to  
the delegate, fun can just take ref parameters.
Full example with definition:

// definition:
void fun(int a, int b, ref int c, void delegate() callback)
{
	if(a == b) { c = 2; callback(); }
	else { c = 1; callback(); }
}

// call:
int var = 0;
fun(9, 9, var) { assert(var == 2); }

this could be taken another step; if variable declarations could be  
allowed in a function call, it can be more localized:
fun(9, 9, int var) { assert(var == 2); }


More information about the Digitalmars-d mailing list