-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Why "let" is important #4015
Comments
Here's the MDN on It's getting support with V8 4.5, which will be released with Chrome 45 stable, which, based on the Chrome 44 stable release date, should land 5-8 weeks from now. Right now, trying to compile with
As of 1.9.3 |
I'll let @jashkenas weight on that one (since coffeescript doesn't have declarators) |
@vendethiel I know, I figured that since "let" is an exception rather than rule, anyway, it'd be explicit. Otherwise, maybe a prefix-symbol? Maybe foo = bar # Standard scope
&baz = bam # Block scope, eg, "let" I'd almost want baz -> bam But I'm not sure of a way it could be disambiguated between "I want a function here" and "I want a block-scoped variable here" |
Maybe break compatibility and automatically decide when to use myVar = "hello"
if myVar is "hello"
myOtherVar = "goodbye" would compile to var myVar = "hello";
if (myVar === "hello") {
let myOtherVar = "goodbye";
} |
I'm sorry. can we please stop with this "let" business, and let it go? ES5 and ES6 force you to think about variable scope every time you write a function. CoffeeScript means that you don't have to. All of the pitfalls you would worry about are automatically avoided. When |
That's pretty much what my comment suggested -- have CoffeeScript decide for you automatically. Of course, as you said, this doesn't need to be the default until most major platforms support it. |
@bjmiller I cannot quite agree given that CS has a weakness that has been pointed out by many, one that it shares, in the genes as it were, with Python, and that is automagic lexical scoping controlled by sameness of names across scopes. For example, when you have f = ->
x = 42 # A
g = ->
x = 108 # B
return x
g()
return x then if the variable ('declared' and) assigned at point B happens to be identified by the same sequence of characters as the one at point A, you invariably get a single JS has the weakness that, should you forget to declare CS is way safer and more convenient that JS, BUT it does totally lack a way to have two independent variables by the same name in a surrounding and a nested scope. This has long be a peeve of mine, in CS as well as in Python. Basically, their rules work well for a lot of cases, but there are cases where you want to distinguish the present scope from the enclosing scopes (actually, the global scope can be distinguished with Just as a strawman, we could say that just as
Granted that f = -> | f = ->
x = 42 | x = 42
g = -> | g = ->
^x = 108 | °x = 108
return ^x | return °x
g() | g()
return x | return x The |
+1 |
Two quick things: I think that CoffeeScript should remain doing what it is for 99% of the cases. Don't try to get too clever, just assume all variables go to the top of their enclosing scope as One exception would be perhaps automatically make for...in and for...of loop declarations use Finally, completely sidestep the issue of never explicitly declaring a variable without initializing and use a new syntax for |
I think much like @samuelhorwitz, however i don't like any proposals for defining let. One great thing on Coffee is to make the code more readable, also for who don't know Coffee or even for who is not a programmer. like Coffee is not about to write fancy JS, it is about to make it simple and better. So, if we really need I should enforce the @samuelhorwitz's proposal of make |
x = true
f = -> x = false # x is not a parameter
do f
console.log x # puts false
x = true
f = (x) -> x = false # x is a parameter
do f
console.log x # puts true And, of course, you can just use a different name, even if it's just |
Yeah @carlsmith, you are right, however to propose to use a function, when you want a local variable is not an overwork with no true meaning? Legibility is about meaning. I only want Just +1 for |
I'm -1 for variable declarations. I think I don't think variable shadowing is a good argument for supporting declarations, a lot of linting tools for various languages will complain about shadowing (explicitly with I do find |
It's worth mentioning again that people just have to accept sometimes that CoffeeScript already has pretty much all the features it's going to ever have. There may be some growth around stuff like modules or async, but new features that change the look and feel of the core language are pretty unlikely. If you concatenate all the new features that different users feel they need to avoid migration to ES6, they are basically asking to overhaul CoffeeScript from the ground up, to make it more modern. I can't see that ever happening wholesale. Someone will have to start again, and design something with modern JavaScript in mind from the start. It's seems more realistic to just expect a new language, with its own ideas, even though any language for the Web will build on ideas from CoffeeScript. |
That is what opensource and GIT forks are for, isn't it? To not completely rewrite stuff when you want to tweak some small features. Even more, this should be in the main fork and users should be able to compile code in such a way with --let option. Why "let by default" should be the main feature of the next generation of CoffeScript?
https://jsfiddle.net/6yrcdaxf/
This is the most logical and expected behavior. Behavior of var is totally unexpected. It only works well if you're not doing any parallelism, or asynchronicity. I came across this problem several times and it is one of the things people hate asynchronous JS coding for. LET behavior, on the other hand is parallelizable and is totally what OpenMP does in such cases when you use it's "#pragma omp parallel for". OpenMP, arguably, is the best parallelization software. Because of such things. Also, the other example from the above Mozilla JS help is also influential.
Let is definitely a more logical behavior in this case too. Regarding browser support, first important thing is that you don't care about that when working with NodeJS. Second is that you don't care much if CoffeScript actually compiles to LET, or if it provides some workaround instead. I think there should be --let-native and --let-emulate directives for those. Just like that, we need a --jsx option for working with ReactJS's https://facebook.github.io/jsx/ , because ReactJS is too good to ignore and too syntactic to code with plain JS. Edit: actually, looking at ECMAScript6, it seems like CoffeeScript is going to die anyway. Modern JS has many great new features and is much easier to write than it was and it now makes sense to use it (and Babel JS) instead. It would be hard for Coffee to catch up with all of the new features coming out as fast as they do (every year now?) and coffee would become too complex and not very beneficial to learn vs the future JS. Edit again: okay, it's probably too early to speak of "dying". Also, with crazy C# people on board, it's not likely JS is going to get really Pythonic. So there's likely to still be a niche for clean -> dirty compiler. |
A language has one feature - the ability to write high-quality, maintainable code. To my eyes coffeescript still does that better than any language on the planet for a very large number of real-world things that real-world developers are actually tasked with working on. |
I agree 100% is great, its syntax is so neat, very readable and expressive and so on. IMHO I wrote this to initiate the community #4078 (comment) I hope it get things moving. PS: and yes, |
👍 Move to "Its just ES6"! |
但是但是,为什么不彻底放弃var而使用let呢? translate from google: |
https://youtu.be/rwm5JLqCpdk?t=17m49s
In CoffeeScript the "let" behavior requires old tricks--a function callback that re-declares the variable to its block each iteration. Simple syntax sugar like -> and => does in addressing scope, would be awesome.
Hopefully not perceived as a duplicate of #3571
The text was updated successfully, but these errors were encountered: