Safe-DS / DSL

Statically checked Data Science programs.
https://dsl.safeds.com
MIT License
14 stars 0 forks source link

Store receiver in variable when generating Python code #1110

Closed lars-reimann closed 5 months ago

lars-reimann commented 5 months ago

Is your feature request related to a problem?

Given this Safe-DS code

@Impure([ImpurityReason.Other]) fun f(param: Any?)

class C() {
    @Pure
    @PythonCall("$param.i($this)")
    fun i(param: Any?) -> result: Boolean
}

pipeline test {
    f(C().i(1));
}

the following Python code is created:

    f(safeds_runner.memoized_static_call(
        "tests.generator.memberAccessWithRunnerIntegration.C.i",
        lambda *_ : 1.i(safeds_runner.memoized_static_call(
            "tests.generator.memberAccessWithRunnerIntegration.C",
            C,
            [],
            {},
            []
        )),
        [safeds_runner.memoized_static_call(
            "tests.generator.memberAccessWithRunnerIntegration.C",
            C,
            [],
            {},
            []
        ), 1],
        {},
        []
    ))

The receiver is computed twice. Even though the call is memoized, we might still have to expensively compute and compare memoization keys. Moreover, the code is hard to read.

Finally, if the receiver cannot be memoized, the generated code is just wrong, since two separate instances are created.

Desired solution

Save the receiver once into a Python variable and reference it as often as needed.

Possible alternatives (optional)

No response

Screenshots (optional)

No response

Additional Context (optional)

This is also needed for #1107.

lars-reimann commented 5 months ago

:tada: This issue has been resolved in version 0.14.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: