www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Lookhead for nested functions

reply Amex <Amex gmail.com> writes:
fails:

void foo()
{
    bar();
    void bar() { }
}

passes:

void foo()
{
    void bar() { }
    bar();
}


I realize this may not be a simple solution in all cases but 
surely it can be make to work in such simple cases?
Jun 13
next sibling parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
 I realize this may not be a simple solution in all cases but 
 surely it can be make to work in such simple cases?
Nope. Consider the following code: int foo() { return bar(); int i = 5; int bar() { return i; } } Nested functions are inextricably bound to function scope, and function scope is strictly sequential.
Jun 13
parent reply Amex <Amex gmail.com> writes:
On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:
 On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
 I realize this may not be a simple solution in all cases but 
 surely it can be make to work in such simple cases?
Nope. Consider the following code: int foo() { return bar(); int i = 5; int bar() { return i; } }
There is nothing wrong with this.
Jun 15
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 15/06/2019 10:16 PM, Amex wrote:
 On Friday, 14 June 2019 at 06:00:13 UTC, FeepingCreature wrote:
 On Friday, 14 June 2019 at 05:35:31 UTC, Amex wrote:
 I realize this may not be a simple solution in all cases but surely 
 it can be make to work in such simple cases?
Nope. Consider the following code: int foo() {   return bar();   int i = 5;   int bar() { return i; } }
There is nothing wrong with this.
Yes there is. i never got initialized, it is effectively =void. Assuming its stored on the stack and not a register. E.g. Here is an example that shows what you're suggesting. int func() { int x; somethingElse(x); int y = 7; return y; } Assembled: int onlineapp.func(): push RBP mov RBP,RSP sub RSP,010h xor EAX,EAX mov -8[RBP],EAX mov EDI,EAX call void onlineapp.somethingElse(int) PLT32 mov ECX,7 mov -4[RBP],ECX mov EAX,ECX leave ret The instructions: mov ECX,7 mov -4[RBP],ECX Never ran and hence who knows what the return value is. If you really want this, it will require a DIP.
Jun 15
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 14.06.19 07:35, Amex wrote:
 fails:
 
 void foo()
 {
     bar();
     void bar() { }
 }
 
 passes:
 
 void foo()
 {
     void bar() { }
     bar();
 }
 
 
 I realize this may not be a simple solution in all cases but surely it 
 can be make to work in such simple cases?
 
 
 
 
If you need mutual recursion for local functions, this hack works: void foo(){ void bar()(){ baz(); } void baz(){ bar(); } }
Jun 14
parent Walter Bright <newshound2 digitalmars.com> writes:
On 6/14/2019 5:50 AM, Timon Gehr wrote:
 If you need mutual recursion for local functions, this hack works:
 
 void foo(){
      void bar()(){ baz(); }
      void baz(){ bar(); }
 }
Using function pointers also works. The point being, the use cases are rare enough that such workarounds are reasonable given the problems of trying to compile forward dependencies.
Jun 14