Okay, who here remembers GOSUB?
Wow, that few? Okay, GOSUB was a feature of BASIC implementations pre VB.Net. It executed a subroutine that was defined
within another routine, sort of like this:
Dim x as Integer
x = 1
Gosub Sub1
x = 0
Sub1:
x = x + 1
Return
If the syntax is a bit off, it's because I haven't even seen a GOSUB in many years. (I'm capitalizing it because I originally learned it in Apple II Basic, where all things were capitalized.) VB.Net did away with it on the grounds that it isn't structured, entirely correctly in my opinion. You see, a subroutine in a GOSUB had the same scope as the parent routine, and so had access to all its variables.
Which brings us back to my previous post, about anonymous methods having access to the variables in the routines in which they are defined, even if they are invoked from outside the method. It occurred to me that this basically replicates the GOSUB. Consider the following:
int i = 0;
Action a = () =>
{
System.Diagnostics.Debug.WriteLine("I am routine 'A'");
i++;
};
Action b = () =>
{
System.Diagnostics.Debug.WriteLine("I am routine 'B'");
i++;
};
// Regular inline code
i = 1;
System.Diagnostics.Debug.WriteLine("I am inline code");
System.Diagnostics.Debug.WriteLine("The value of i is currently {0}",i);
a();
b();
System.Diagnostics.Debug.WriteLine("I am inline code again");
System.Diagnostics.Debug.WriteLine("And the value of i is now {0}",i);
The only way in which this differs in function from the Basic above is that the Action variables have to have anonymous methods assigned to them before they are invoked, rather than after as was the norm (and perhaps a requirement) in Basic.
Welcome back, GOSUB.