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 a const
. This thread is not for debating automatic let
assignment, or whether we should change other things related to variable assignment. Just const
. 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 a const
. So:
foo := 42
becomes:
const foo = 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, with var
. Even though using :=
would cause const
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 since const
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?