Ona Script Closures #41

Closed
opened 2023-09-02 21:11:20 +02:00 by kayomn · 1 comment
Owner

Add the ability to reference variables from the outer scope and have the compiler / runtime automatically resolve references which escape the local stack lifetime.

Closures could be implemented via Lua-like upvalues, however given that the program has a full AST representation this could be taken advantage of to create site-specific lexical scopes that only contain the closed-on variables in a far less runtime heavy manner.

My anticipation is that the following code:

let message = "This is a closure"

let pr = lambda (str):
  @print(message)
  @print(str)
end

pr("Hello world")

May be restructured into the equivalent logic by the AST.

let message = "This is a closure"

let pr = _create_closure({message}, lambda (_closure_context, str):
  @print(_closure_context[0])
  @print(str)
end)

pr("Hello world")
Add the ability to reference variables from the outer scope and have the compiler / runtime automatically resolve references which escape the local stack lifetime. Closures could be implemented via Lua-like upvalues, however given that the program has a full AST representation this could be taken advantage of to create site-specific lexical scopes that only contain the closed-on variables in a far less runtime heavy manner. My anticipation is that the following code: ```ruby let message = "This is a closure" let pr = lambda (str): @print(message) @print(str) end pr("Hello world") ``` May be restructured into the equivalent logic by the AST. ```ruby let message = "This is a closure" let pr = _create_closure({message}, lambda (_closure_context, str): @print(_closure_context[0]) @print(str) end) pr("Hello world") ```
kayomn added this to the (deleted) milestone 2023-09-02 21:11:20 +02:00
kayomn self-assigned this 2023-09-02 21:11:20 +02:00
kayomn changed title from Closures to Ona Script Closures 2023-09-02 21:15:50 +02:00
Author
Owner

Resolved as part of #44.

Resolved as part of #44.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: kayomn/ona#41
No description provided.