www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Forward reference not working in nested functions

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Is this a known bug or deliberate limitation?

	void func(T)(T data) {
		void subfunc1() {
			process(data);
			if (recurse) subfunc2(); // Error: subfunc2 undefined
		}

		void subfunc2() {
			process(data);
			if (recurse) subfunc1(); // Error: subfunc1 undefined
		}

		subfunc1();
	}


T

-- 
Heads I win, tails you lose.
Dec 12 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-12-12 19:03, H. S. Teoh wrote:
 Is this a known bug or deliberate limitation?

 	void func(T)(T data) {
 		void subfunc1() {
 			process(data);
 			if (recurse) subfunc2(); // Error: subfunc2 undefined
 		}

 		void subfunc2() {
 			process(data);
 			if (recurse) subfunc1(); // Error: subfunc1 undefined
 		}

 		subfunc1();
 	}

It's basically the same thing as you need to declare variables in correct order in a function. -- /Jacob Carlborg
Dec 12 2012
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 12/12/2012 07:03 PM, H. S. Teoh wrote:
 Is this a known bug or deliberate limitation?

 	void func(T)(T data) {
 		void subfunc1() {
 			process(data);
 			if (recurse) subfunc2(); // Error: subfunc2 undefined
 		}

 		void subfunc2() {
 			process(data);
 			if (recurse) subfunc1(); // Error: subfunc1 undefined
 		}

 		subfunc1();
 	}


 T

Both.
Dec 12 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/12/2012 09:03 PM, Timon Gehr wrote:
 On 12/12/2012 07:03 PM, H. S. Teoh wrote:
 Is this a known bug or deliberate limitation?

     void func(T)(T data) {
         void subfunc1() {
             process(data);
             if (recurse) subfunc2(); // Error: subfunc2 undefined
         }

         void subfunc2() {
             process(data);
             if (recurse) subfunc1(); // Error: subfunc1 undefined
         }

         subfunc1();
     }


 T

Both.

(A silly workaround is the following, but I do not know if this has been documented since it has been discovered: void func(T)(T data) { void subfunc1()() { process(data); if (recurse) subfunc2(); // Ok. } void subfunc2() { process(data); if (recurse) subfunc1(); // Ok. } subfunc1(); } )
Dec 12 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 (A silly workaround is the following, but I do not know if this 
 has been documented since it has been discovered:

The workaround I use was found by Don, is to the define an inner static struct with static methods. Bye, bearophile
Dec 12 2012