digitalmars.D.learn - Workaround for foreward declaration ?
- Peter Sommerfeld (31/31) Mar 03 2013 According to http://dlang.org/ctod.html foreward declarations
- Jonathan M Davis (7/42) Mar 03 2013 AFAIK, you have to put them outside for this. The order of both function...
- Peter Sommerfeld (4/9) Mar 03 2013 Thanks David! Should be mentioned somewhere though. Well, probably is,
According to http://dlang.org/ctod.html foreward declarations are not needed because functions can be defined in any order. But that seems not to be true for inner functions. A somewhat artificial example: ----------------------------- import std.stdio; void main(string[] args){ int count; // void foo() -- unlike in C in D not possible void bar(){ ++count; writeln("bar"); foo(); // ERROR: undefined identifier foo } void foo(){ writeln("foo"); if(count){ writeln("foo again"); return; } bar(); } foo(); } --------------------------------------- Is there a workaround for such a situation or do I have to put everything outside the enclosing function. Peter
Mar 03 2013
On Sunday, March 03, 2013 15:39:38 Peter Sommerfeld wrote:According to http://dlang.org/ctod.html foreward declarations are not needed because functions can be defined in any order. But that seems not to be true for inner functions. A somewhat artificial example: ----------------------------- import std.stdio; void main(string[] args){ int count; // void foo() -- unlike in C in D not possible void bar(){ ++count; writeln("bar"); foo(); // ERROR: undefined identifier foo } void foo(){ writeln("foo"); if(count){ writeln("foo again"); return; } bar(); } foo(); } --------------------------------------- Is there a workaround for such a situation or do I have to put everything outside the enclosing function.AFAIK, you have to put them outside for this. The order of both function calls and import declarations matters inside of a function even though it doesn't matter outside. In general, nested functions are more limited (e.g. if they're templated, you can't instantiate them with different arguments - you only get one instantation). - Jonathan M Davis
Mar 03 2013
Jonathan M Davis wrote:AFAIK, you have to put them outside for this. The order of both function calls and import declarations matters inside of a function even though it doesn't matter outside. In general, nested functions are more limited (e.g. if they're templated, you can't instantiate them with different arguments - you only get one instantation).Thanks David! Should be mentioned somewhere though. Well, probably is, somewhere ... Peter
Mar 03 2013