- Notifications
You must be signed in to change notification settings - Fork4
CS2 Discussion: Features: const assignment operator #31
Description
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 aconst
. This thread isnot 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:
foo :=42
becomes:
constfoo=42;
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, withvar
. 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 syntaxthat isn’t a breaking change. Simply following ES2015, with a syntax likeconst 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?