ponylang / ponyc

Pony is an open-source, actor-model, capabilities-secure, high performance programming language
http://www.ponylang.io
BSD 2-Clause "Simplified" License
5.74k stars 415 forks source link

Nested lambdas can't auto-capture free references from the outer scope. #1708

Open jemc opened 7 years ago

jemc commented 7 years ago

As of #1648, the following works:

actor Main
  new create(env: Env) =>
    {() =>
      env.out.print("Hello, world!")
    }()
Building builtin -> /home/jemc/1/code/gitx/ponyc/packages/builtin
Building . -> /home/jemc/1/code/gitx/ponyc/test.jemc
Generating
 Reachability
 Selector painting
 Data prototypes
 Data types
 Function prototypes
 Functions
 Descriptors
Optimising
Writing ./test.jemc.o
Linking ./test.jemc
Hello, world!

But the following doesn't:

actor Main
  new create(env: Env) =>
    {() =>
      {() =>
        env.out.print("Hello, world!")
      }()
    }()
Building builtin -> /home/jemc/1/code/gitx/ponyc/packages/builtin
Building . -> /home/jemc/1/code/gitx/ponyc/test.jemc
Error:
/home/jemc/1/code/gitx/ponyc/test.jemc/test.pony:6:9: cannot capture "env", variable not defined
        env.out.print("hello world")
        ^
Error:
/home/jemc/1/code/gitx/ponyc/test.jemc/test.pony:6:9: can't find declaration of 'env'
        env.out.print("hello world")

If lambdas can automatically capture free references from the immediately surrounding scope, I'd expect (principle of least surprise) that they can also do so with free references from an outer scope surrounding that scope, with the surrounding scope being made to capture the reference intermediately.

That is, I'd expect this to happen implicitly:

actor Main
  new create(env: Env) =>
    {()(env) =>
      {()(env) =>
        env.out.print("Hello, world!")
      }()
    }()
jemc commented 7 years ago

Discussed on sync call - @sylvanc agrees it should work, and it's an implementation bug.

jemc commented 7 years ago

Adding the needs discussion tag here so we can check the status of this during the next sync call.

SeanTAllen commented 7 years ago

I wasn't aware this issue existed.

SeanTAllen commented 7 years ago

@sylvanc any progress on this?