www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Can't Forward Reference Nested Functions

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Linux (Fedora Core 1), DMD 0.110

I can't call nested functions that I haven't defined yet.

 void main() {
   nestedFunc();
  
   void nestedFunc() {};
 }
DMD complains with the following error:
 cant_forward_reference_nested_funcs_bug.d(2): undefined identifier nestedFunc
 cant_forward_reference_nested_funcs_bug.d(2): function expected before (), not
'int'
Jan 18 2005
parent reply J C Calvarese <jcc7 cox.net> writes:
In article <csk4ne$2njc$2 digitaldaemon.com>, Russ Lewis says...
Linux (Fedora Core 1), DMD 0.110

I can't call nested functions that I haven't defined yet.

 void main() {
   nestedFunc();
  
   void nestedFunc() {};
 }
DMD complains with the following error:
 cant_forward_reference_nested_funcs_bug.d(2): undefined identifier nestedFunc
 cant_forward_reference_nested_funcs_bug.d(2): function expected before (), not
'int'
Perhaps the error message could be improved, but the spec mentions that this code isn't allowed: "Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other." (http://www.digitalmars.com/d/function.html) Hopefully someday we'll be able to use nested functions before they are defined, but at least this limitation is mentioned in the spec. jcc7
Jan 19 2005
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
J C Calvarese wrote:
 Perhaps the error message could be improved, but the spec mentions that this
 code isn't allowed:
 
 "Unlike module level declarations, declarations within function scope are
 processed in order. This means that two nested functions cannot mutually call
 each other." (http://www.digitalmars.com/d/function.html)
 
 Hopefully someday we'll be able to use nested functions before they are
defined,
 but at least this limitation is mentioned in the spec.
Hmmm. Didn't notice that. Well, I guess that it's fair if it's documented :/ I am very curious though, Walter, what the reason for this is. Just speaking as a programmer, I'd be fascinated to understand why this requirement exists. It would seem (from the outside) that whatever code you use use handle forward references in general could be leveraged to also handle this case.
Jan 19 2005
parent "Walter" <newshound digitalmars.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:csm3d1$2oda$1 digitaldaemon.com...
 I am very curious though, Walter, what the reason for this is.  Just
 speaking as a programmer, I'd be fascinated to understand why this
 requirement exists.  It would seem (from the outside) that whatever code
 you use use handle forward references in general could be leveraged to
 also handle this case.
To fix it, the semantic pass for function bodies would have to be rewritten to be multipass, and it seems a low priority to do that.
Jan 19 2005