Elegant syntax for dynamic vs. static delegates

Russell Lewis webmaster at villagersonline.com
Mon Nov 3 11:22:12 PST 2008


Objective 1: Make the heap vs. stack variables explicit
Objective 2: Make it impossible to return or store a static (stack) delegate
Objective 3: Don't require decorators on lambda expressions.

Solution:
- Variables are on stack by default
- Use modifier "heap" to put a variable on the heap
- Delegates can be normal (storable) or "scope" (can't live beyond the 
scope of our function, and the type is inferred BASED ON WHAT VARIABLES 
YOU ACCESS.


EXAMPLE CODE

void foo(scope void delegate()) {...}
void bar(void delegate()) {...}

void main()
{
   int a;
   heap int b;

   foo({ a = 1; });	// legal.
   bar({ b = 2; });	// legal.  bar could store dg, but b is on heap
   foo({ b = 3; });	// legal.  ok to pass non-scope dg to
			//         scope argument
   bar({ a = 4; });	// SYNTAX ERROR
			// delegate is scope b/c a is on stack, but
			// argument to bar isn't scope.
}

END CODE


Thoughts?



More information about the Digitalmars-d mailing list