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

python: passing function reference introduces unnecessary closure #3877

Open
joprice opened this issue Aug 15, 2024 · 0 comments
Open

python: passing function reference introduces unnecessary closure #3877

joprice opened this issue Aug 15, 2024 · 0 comments

Comments

@joprice
Copy link
Contributor

joprice commented Aug 15, 2024

Description

When using the python backend and passing a function reference, it gets wrapped in an extra closure, with the outer function environment's arguments appended. This prevents passing a function to constructors like multiprocessing.Process, which require the function to be pickleable, failing with AttributeError: Can't pickle local object.

Repro code

[Please provide the F# code to reproduce the problem or a link to the REPL.
Ideally, it should be possible to easily turn this code into a unit test.](https://fable.io/repl/#?code=DYUwLgBA5hAUCUEC8d4Cg2kgJwK4DsIAzZYhDLCXABwBMBDMECAfXuxmTQh70KiA&html=Q&css=Q)

Expected and actual results

When passing a function as an argument

let g () = ()

let run f = f()

let update _arg  =
   run g

the function should be called directly as in the JS output:

export function g() {
}

export function run(f) {
    return f();
}

export function update(_arg) {
    run(() => {
        g();
    });
}

Instead, a local function _arrow1 is introduced with an extra _arg: Any=_arg param:

from collections.abc import Callable
from typing import (Any, TypeVar)

__A = TypeVar("__A")

def g(__unit: None=None) -> None:
    pass


def run(f: Callable[[], __A]) -> __A:
    return f(None)


def update(_arg: Any | None=None) -> None:
    def _arrow1(__unit: None=None, _arg: Any=_arg) -> None:
        g()

    run(_arrow1)

Related information

  • Fable version: 4.19.3
  • Operating system: OSX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant