Nested functions should be exempt from sequential visibility rules

Xinok xinok at live.com
Wed Apr 4 09:21:47 PDT 2012


On Tuesday, 3 April 2012 at 12:13:00 UTC, Timon Gehr wrote:
> On 04/03/2012 02:00 PM, Nick Sabalausky wrote:
>> "Timon Gehr"<timon.gehr at gmx.ch>  wrote in message
>> news:jlej27$mvi$1 at digitalmars.com...
>>>
>>> This is the right way to work around this issue. It works now 
>>> and does not
>>> imply any kind of overhead at runtime:
>>>
>>> void foo(){
>>>     void a()(){ ... }
>>>     void b()  { ... }
>>> }
>>>
>>
>> 1. How the heck does that work?
>>
>
> Symbol lookup is done upon the first instantiation of the local 
> template.

Currently, it prints 365. But remove the commented line, and it 
prints 500 twice. But it's a minor issue at worst that's easy to 
avoid, and this is a very simple workaround.

int two()
{
	return 500;
}

void main()
{
	int x = 365;
	
	int one()()
	{
		return two();
	}
	
	// writeln(one());
	
	int two()
	{
		return x;
	}
	
	writeln(one());
}


I found another solution involving templates, though not as 
convenient:

template foo()
{
	int one(){
		return two();
	}
	
	int two(){
		return x + y;
	}
	
	int y = 12;
}

void main()
{
	int x = 365;
	mixin foo;
	writeln(one());
}



More information about the Digitalmars-d mailing list