-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: const assignment operator #31
Comments
I prefer matching JavaScript here. It's less confusing to newcomers, matches compiled output exactly, etc. Seems preferable for a minor ES6 feature of limited utility. As I would much rather see |
I take it back, I didn’t notice that |
I tend to use |
I can see the value of I would still prefer |
I'll drop my opinion on this here by way of completeness (#30). I strongly believe that the concept of an exposed |
that's not true for many people; see the discussion in #1 .
that's not true for many coffeescript developers either 😉 |
How parseable would |
If we could somehow automatically choose What’s lost by automatic I think we could, potentially, automatically assign variables with |
I don't quite see the point in an inferred const? (As discussed elsewhere). If people want pretty output they can use decaffeinate. |
Personally, I don't care about any theoretical performance benefit or something like that by using |
So @rattrayalex and @lydell want the explicit “I want the compiler to fail if I reassign this variable” while @DomVinyard and @greghuc in #30 want to preserve “I don’t need to think about how my variables are assigned.” We get the former with |
👍 for explicit Implicit const is not worth implementing. There is absolutely no benefit to it. |
+1 for explicit. For both const and let. Unfortunately it seems that const refers only to the reference of the variable not changing, rather than variable immutability. Perhaps |
@JimPanic there is actually a benefit to implicit if (true) {
var a = 1;
let b = 2;
const c = 3;
}
console.log(a); // 1
console.log(b); // Uncaught ReferenceError: b is not defined
console.log(c); // Uncaught ReferenceError: c is not defined Also theoretically runtimes should be more performant with |
I’m starting to think it doesn’t make sense to implement only |
I’m going to close this in favor of #35, unless anyone objects. |
I object 😉
Regardless of whether |
How would you deal with the side effect that outputting |
A line of documentation 😉 |
The ability to declare some variables as block, and some as lexical, is a "feature" of JavaScript, bless-its-heart. |
And the number of times when you really don't want coffeescript's current scoping, and really want block scoping instead, and can't use const, is probably extremely low. |
I think if we introduce into CoffeeScript the ability to declare a block-scoped variable, we can’t force it to be unreassignable. If we give people Maybe the times when someone would want |
In theory, that's true. In practice, we can give the one without the other, and very few people will run into difficulties. |
However, if you'd like to propose completely replacing ... in any case, I personally think this thread should be kept alive for discussion, regardless of whether you think it will ultimately make sense to merge. |
I disagree that the amount of times people would want I’m not convinced we should mess with variable assignment. Perhaps one |
We definitely don't want to take away the ability to use var when they expect it. Especially when dealing with closures. |
Keep in mind I'm not any good at writing transpilers: why not just let people use
|
@jcollum there’s no reason the transpiler couldn’t. It was a core design decision of CoffeeScript, though, that developers wouldn’t have to think about variable declaration. That’s why CoffeeScript groups together all variables used in a scope to a Not everyone agrees with this design decision, clearly. And they may have more ammo to their argument since ES2015 introduced |
I like the design principle of "Use a default but allow people to override it". I'd prefer not to use backticks to override it. 2¢ |
See this comment. I really don’t think allowing |
Branching off from #1 and #30, I thought it would be good to have a thread dedicated to discussing the proposed new operator for explicitly declaring and assigning a variable as a
const
. This thread is not for debating automaticlet
assignment, or whether we should change other things related to variable assignment. Justconst
. For other topics, please comment on other issue threads.The consensus from #1 seems to be to add an
:=
operator that works like=
, but declares and assigns a variable as aconst
. So:becomes:
This has the advantage that it’s not a breaking change; currently
:=
fails to compile. There is no need to support?:=
, since by definition constants can’t be reassigned.Nothing else would be changed by adding this new operator. Normal assignment is handled as it is today, with
var
. Even though using:=
would causeconst
to be in the generated output, this feature is “opt in” like modules and the same warning would apply about transpiling CoffeeScript’s output.If people like the idea but disagree with the syntax, I invite you to propose an alternate syntax that isn’t a breaking change. Simply following ES2015, with a syntax like
const foo = 42
, is an option sinceconst
is already a reserved word in CoffeeScript.So I think the debate is: should we add this at all? And if so, this syntax or some other backwards-compatible syntax?
The text was updated successfully, but these errors were encountered: