Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to define functions with copying closures #731

Closed
evhub opened this issue Apr 21, 2023 · 3 comments
Closed

Add a way to define functions with copying closures #731

evhub opened this issue Apr 21, 2023 · 3 comments

Comments

@evhub
Copy link
Owner

evhub commented Apr 21, 2023

Should have a way to create a function that copies the locals() in the calling function at the time of creation rather than simply grabbing a reference to them. For lambdas, will probably only be for statement lambdas.

@evhub evhub added the feature label Apr 21, 2023
@evhub evhub added this to the v3.0.0 milestone Apr 21, 2023
@evhub
Copy link
Owner Author

evhub commented Apr 21, 2023

After a bunch of testing, it looks like we should be able to get this to work by compiling (exact syntax still TBD)

def outer_func():
    funcs = []
    for x in range(10):
        closure def inner_func():
            return x
        funcs.append(inner_func)
    return funcs

to something like

def outer_func():
    funcs = []
    for x in range(10):
        _inner_func_vars = globals().copy() | locals().copy()
        exec("""def inner_func():
    return x""", _inner_func_vars)
        inner_func = _inner_func_vars["inner_func"]
        funcs.append(inner_func)
    return funcs

@evhub evhub changed the title Add a way to define lambdas with full closures Add a way to define functions with copying closures Apr 21, 2023
@evhub
Copy link
Owner Author

evhub commented Apr 21, 2023

Some syntax options:

closure def inner_func() = x
closed def inner_func() = x
bind def inner_func() = x
copyclosure def inner_func() = x

@evhub
Copy link
Owner Author

evhub commented Apr 21, 2023

Syntax that we're going with for this is

copyclosure def inner_func() = x

evhub added a commit that referenced this issue Apr 21, 2023
@evhub evhub added the resolved label Apr 21, 2023
@evhub evhub closed this as completed Apr 21, 2023
@evhub evhub mentioned this issue May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant