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

assigning _ in the repl has side effects #5431

Closed
calidion opened this issue Feb 25, 2016 · 14 comments
Closed

assigning _ in the repl has side effects #5431

calidion opened this issue Feb 25, 2016 · 14 comments
Labels
repl Issues and PRs related to the REPL subsystem.

Comments

@calidion
Copy link

In Nodejs REPL env, where var _ = require('lodash') failed to be assigned, but abc can.

> var _ = require('lodash');
undefined
> _
undefined
> var abc = require('lodash');
undefined
> abc
{ [Function: lodash]
  templateSettings: 
   { escape: /<%-([\s\S]+?)%>/g,
     evaluate: /<%([\s\S]+?)%>/g,
     interpolate: /<%=([\s\S]+?)%>/g,
     variable: '',
     imports: { _: [Circular] } },
@mscdex mscdex added the repl Issues and PRs related to the REPL subsystem. label Feb 25, 2016
@mscdex
Copy link
Contributor

mscdex commented Feb 25, 2016

In the repl documentation it mentions that the _ variable is special and always refers to the result of the last expression. In other words, you cannot use _ in the repl for your own use, as it will get overwritten every time you execute a "line" of code.

@mscdex mscdex added the invalid Issues and PRs that are invalid. label Feb 25, 2016
@calidion
Copy link
Author

the use of _ is so common for lodash or underscore. I don't have any reason why _ is chosen to be the default result. . would be far more acceptable than _

@vkurchatkin
Copy link
Contributor

. is not a valid identifier

@rvagg
Copy link
Member

rvagg commented Feb 25, 2016

Just out of interest I thought I've do some provenance work on this

_ was introduced in repl.js back in the 25th of September 2009 @ 7674bd5

Underscore was born almost exactly one month later, on the 26th of October 2009 @ jashkenas/underscore@02ede85

Perhaps we should go argue to Underscore and lodash that they should change to something that won't conflict with Node?

@vkurchatkin
Copy link
Contributor

FWIW Chrome console uses $_ for this and it works the same way

@bnoordhuis
Copy link
Member

To add to what @rvagg said, the underscore's behavior was copied from the python and ruby REPLs.

@calidion
Copy link
Author

I think __ or __last or __xxx would be much acceptable where __ is known to be system reserved.

@bnoordhuis
Copy link
Member

It's been _ for over half a decade now, it's not going to change.

@vkurchatkin
Copy link
Contributor

We could try two things:

  • warn on assignment to global _;
  • disable this behaviour when global _ is defined;

@calidion
Copy link
Author

@vkurchatkin
I would say that is a very good idea.
💯

@rvagg
Copy link
Member

rvagg commented Feb 25, 2016

I like that suggestion @vkurchatkin, would lead to fewer surprises like the one that prompted this issue

@silverwind
Copy link
Contributor

Why not just disable _ functionality with a warning once it's being assigned to? Not being able to paste lodash examples in the REPL is not ideal, imho.

@silverwind silverwind reopened this Feb 25, 2016
@silverwind silverwind removed the invalid Issues and PRs that are invalid. label Feb 25, 2016
@Fishrock123 Fishrock123 changed the title strange bug assigning _ in the repl has side effects Feb 25, 2016
@princejwesley
Copy link
Contributor

I suggest to add an entry in help command like below

node 🙈 ₹  node
> .help
break   Sometimes you get stuck, this gets you out
clear   Alias for .break
exit    Exit the repl
help    Show repl options
load    Load JS from a file into the REPL session
save    Save all evaluated commands in this REPL session to a file
_       Bound to last successfully evaluated expression
>

@rvagg
Copy link
Member

rvagg commented Feb 26, 2016

see #5438 for one option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

8 participants