Lambdas, Nested Functions, and Blocks (2021)

(thephd.dev)

36 points | by zaikunzhang 3 days ago

3 comments

  • PaulHoule 12 hours ago
    I always thought that if you wanted variables to be able to escape the function they were defined in you have to allocate them on the heap and then you need some kind of garbage collection.
    • ori_b 11 hours ago
      You can also have some kind of heapify/free, with undefined behavior if you get it wrong.

      I think the C standards committee needs to figure out memory safety before adding any new features that are likely to interact with it.

      • PaulHoule 9 hours ago
        So you're saying the runtime mallocs the pointer to the closure and it's your responsibility to free it when you don't need it anymore?
        • ori_b 8 hours ago
          Kinda. Here's an example:

               void
               mkclosure(int x)
               {
                   int x;
                   void fn(void){ return x + 1; }
                   return fnheapify(fn);
               }
          
               void
               useclosure(void)
               {
                   void (^fn)(void) = mkclosure(42);
                   fn();
                   fnfree(fn);
               }
  • remexre 12 hours ago
    Are there any implementations of this lambdas proposal on top of any production-quality compilers? Or are there some updates on how this is doing in committee?
  • mwkaufma 5 hours ago
    "Unfortunately, many compiler authors would absolutely give our recommended advice a gigantic middle finger."

    Simmer down, now.