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

erl_eval: Support the fun Name/Arity syntax for creating a fun #8987

Open
wants to merge 1 commit into
base: maint
Choose a base branch
from

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Oct 25, 2024

Attempting to create a fun referering to either an auto-imported BIF or a local fun defined in shell would fail:

1> fun is_atom/1.
** exception error: undefined function erl_eval:is_atom/1
2> id(I) -> I.
ok
3> fun id/1.
** exception error: undefined function erl_eval:id/1

This pull request adds support for defining a fun based on either an auto-imported BIF:

1> F = fun is_atom/1.
fun erlang:is_atom/1
2> F(a).
true
3> F(42).
false

Or on a local function defined in the shell:

1> I = fun id/1.
#Fun<erl_eval.42.18682967>
2> I(42).
** exception error: undefined shell command id/1
3> id(I) -> I.
ok
4> I(42).
42

As shown in the example, it not necessary that the local fun is defined at the time the fun is created; only that it is defined when the fun is called.

@bjorng bjorng added team:VM Assigned to OTP team VM fix testing currently being tested, tag is used by OTP internal CI labels Oct 25, 2024
@bjorng bjorng requested a review from lucioleKi October 25, 2024 08:21
@bjorng bjorng self-assigned this Oct 25, 2024
Attempting to create a fun referering to either an auto-imported
BIF or a local fun defined in shell would fail:

    1> fun is_atom/1.
    ** exception error: undefined function erl_eval:is_atom/1
    2> id(I) -> I.
    ok
    3> fun id/1.
    ** exception error: undefined function erl_eval:id/1

This commit adds support for defining a fun based on either
an auto-imported BIF:

    1> F = fun is_atom/1.
    fun erlang:is_atom/1
    2> F(a).
    true
    3> F(42).
    false

Or on a local function defined in the shell:

    1> I = fun id/1.
    #Fun<erl_eval.42.18682967>
    2> I(42).
    ** exception error: undefined shell command id/1
    3> id(I) -> I.
    ok
    4> I(42).
    42

As shown in the example, it not necessary that the local fun is
defined at the time the fun is created; only that it is defined when
the fun is called.

Closes erlang#8963
@bjorng bjorng force-pushed the bjorn/stdlib/erl_eval/GH-8963/OTP-19322 branch from 2d41b4a to 79b0e0d Compare October 25, 2024 08:22
Copy link
Contributor

github-actions bot commented Oct 25, 2024

CT Test Results

    2 files     95 suites   1h 9m 20s ⏱️
2 155 tests 2 107 ✅ 48 💤 0 ❌
2 514 runs  2 464 ✅ 50 💤 0 ❌

Results for commit 79b0e0d.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@bjorng bjorng linked an issue Oct 25, 2024 that may be closed by this pull request
Copy link
Contributor

@lucioleKi lucioleKi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good!

@robertoaloi
Copy link
Contributor

Nice, thanks for fixing that!

@michalmuskala
Copy link
Contributor

I'm not sure if there's some work necessary in the debugger to make it work there as well since it has a (partial?) fork of the evaluator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Defining a fun for a BIF fails in the shell with undef
4 participants