On 10/17/20 8:28 AM, NonNull wrote:
On Friday, 16 October 2020 at 21:28:18 UTC, Steven Schveighoffer wrote:
Inner functions have benefits:

1. They are only accessible inside the function. Which means you only have to worry about correctness while INSIDE that function.
2. inner functions have access to the outer function's stack frame.

Often, I use inner functions to factor out a common piece of code that I don't want to have to write multiple times in the same function.

-Steve

How can you write two inner functions that call each other? (Recursively)

I thought of the following method just now. Yes, there are lambdas but who cares? :) (Besides, 'a' can be defined as a proper function below.)

import std.range;

void foo(string s) {
  // b is not initialized yet
  void delegate() b;

  // a is initialized
  auto a = {
    while (!s.empty) {
      s.popFront();
      b();
    }
  };

  // Set b to a lambda
  b = {
    while (!s.empty) {
      s.popBack();
      a();
    }
  };

  a();
}

void main() {
  foo("hello");
}

Ali

Reply via email to