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 support for "_" special variable in REPL mode to store last expression value #570

Merged
merged 1 commit into from
Dec 26, 2024

Conversation

vt128
Copy link
Contributor

@vt128 vt128 commented Dec 26, 2024

This Pull Request implements the feature request to support the _ special variable in REPL mode, which stores the value of the last evaluated expression. This enhancement aligns the Starlark REPL behavior more closely with Python's REPL, improving the interactive experience for users. Fixed issue #569.

Changes Made:

  • In repl.go, after each successful expression evaluation, the result is stored in the _ variable within the globals dictionary.
  • The _ variable is only updated when an expression is evaluated, not after executing statements.
  • If an error occurs during expression evaluation, the _ variable remains unchanged.
  • No changes are made to script execution outside of REPL mode.

Benefits:

  • Improves interactive debugging and experimentation workflow.
  • Provides consistency with Python's REPL behavior, making it more intuitive for Python developers.
  • Allows users to easily access the result of the last expression without explicitly assigning it to a variable.

Example Usage:

>>> 2 + 3
5
>>> _
5
>>> _ * 2
10
>>> _
10

Testing:

  • Verified that the _ variable correctly stores the value of the last evaluated expression.
  • Ensured that _ remains unchanged after executing statements or if an error occurs during evaluation.
  • Confirmed that accessing _ before any expression has been evaluated results in a NameError, consistent with Python's REPL.

Copy link

google-cla bot commented Dec 26, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@adonovan adonovan merged commit 8dfa5b9 into google:master Dec 26, 2024
13 checks passed
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

Successfully merging this pull request may close these issues.

2 participants