Rethinking the use of let for immutable bindings
#5899
Replies: 1 comment
-
|
Important point of clarification: // `a` is an immutable value
let a: i32 = 0;
//`b` and `c` are immutable values
let (b: i32, c: i32) = (0, 0);
// `d` is an immutable value, but `e` is a mutable variable
let (d: i32, var e: i32) = (0, 0);
// `f` and `g` are mutable elements of an anonymous object
let var (f: i32, g: i32) = (0, 0);
// `h` is an immutable value, and `i` is a mutable reference to `g`
let (h: i32, ref i: i32) = (f, g);I do think this has caused confusion, but I don't think changing the spelling of Note also that I also don't think this question is ripe to revisit yet, because this whole area is still in flux. For example, we very recently introduced a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to reopen a discussion around the use of
letfor immutable bindings in Carbon. While this has been touched on in Discussion #1497 and Issue #1578, those discussions are over two years old. Given the language and community have evolved since then, I believe it's worth re-evaluating this decision.Summary
Carbon currently uses the following syntax:
Here,
letdefines an immutable binding, andvardefines a mutable one. I'm proposing thatconst(or another clearer keyword) might be a better choice thanletfor immutable bindings, and that the community should revisit this part of the language design.Why reconsider this?
1. Common expectations
letis generally associated with variable definitions in many popular languages, it doesn't intuitively convey immutability or constant values. This can lead to confusion for users who expect a keyword that more explicitly signals that a binding is immutable. Using let for immutable bindings may therefore weaken clarity and go against common expectations around the concept of constants.constto signify immutability, although it has already been criticized by carbon leadersletas meaning immutable.2. Clarity and readability
letis a verb, while other definition and declaration keywords (var,fn, ...) are nouns. This may make the keyword feel syntactically inconsistent, especially when considering future extensibility and teachability of the language.What this discussion is not about
constsemantics at runtime or deep immutability.It is purely a discussion about naming: whether
letis the best keyword to indicate immutability in source code.Request for feedback
I'd like to hear your thoughts on:
lethas caused confusion in your experience with Carbon.constwould make the code easier to read or teach.If there’s enough support or interest, I’m happy to collaborate on a design proposal or work with others to take this further.
Thanks for considering.
Beta Was this translation helpful? Give feedback.
All reactions