-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Are 'variable bindings' valuable? #276
Comments
Thank you for the detailed and thoughtful issue! ❤️ I am not confident in the subtleties involved here, so I'm going to let @steveklabnik speak to this! |
I prefer speaking about bindings. There's other important things bound by introducing a variable.
(I outlined such a case here: https://lobste.rs/s/oxuwzf/rust_vs_c_fine_grained_performance/comments/0bhl8y#c_0bhl8y) To summarise:
is very different from
I prefer speaking about If we talk about introducing a "binding", this makes is clearer that additional things happen by saying "let". |
I think of these terms as being interchangeable, even though I've been told they are not. I think previous versions of the book used these terms in an ambiguously interchangeable way as well. @skade's comment about illuminating what else is created by the |
I've always considered a variable binding to be syntactic and a variable to be semantic; aka. |
Yes, that makes sense to me also - using "binding" to mean "the creating of the variable with a |
I disagree. The consistent & orthogonally useful terminology I prefer to use is:
|
The concepts of free and bound variables are relevant to imperative programming languages, it's just that in basically all compiled/checked languages, all variables are bound. I believe that technically, binding is binding the name to the variable, not a value to variable. I.e., To extend eddyb's example: one name, one variable, two bindings: |
@nrc Yeah, although it was getting complex enough to also add the distinctions between "initializing through pattern binding" and "initialization through assignment", which seem unique to Rust. |
All of this makes sense, but when we're talking about how the book should explain it, how much does this terminology help users understand the language? How often do users need to talk about |
@withoutboats I find "bindings" to be mostly useful when talking about patterns specifically. |
I hope it is rare that you have to use really precise terminology. But what I think is important is avoiding the wrong terminology. We might choose to only use variable or only use binding or whatever, but we should be very careful to avoid using binding to mean variable (or whatever) so that users are not confused when they google a term or if they are familiar with from another language. That is why I think it is important to have a precise understanding of the terms, even if they are only rarely used in the book. |
It was verbose forming multiple questions, so I'm asserting... Please correct me if I'm wrong. let x: u32; //a variable of type u32 is created and bound to the symbolic name x
x = 1; //the value 1 is assigned to the variable x
//technically, the assignment operator is applied to x with a parameter of 1 is equivalent to: Therefore
not:
Of course, they optimise to the same thing, but I think it's an important distinction. let (x, y) = (1u32, 2u32);
let (a, b) = (x, y);
Because a variable is created in every variable binding: variable binding == variable declaration. The fact that temporaries are optimised away is orthogonal. |
This, of course, only holds in irrefutable patterns. OTOH, each pattern in a |
I believe we're trying to say "variable binding" throughout. If anyone finds anyplace that the book is incorrect, I have some heart emojis for a pull request!! ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ |
@carols10cents Only when referring to the syntactical leaf of a pattern? |
I have no idea what is right or wrong anymore, which is why I'd love if someone could fix any spots that are wrong :) |
@carols10cents I think the thrust of this conversation is that the way the book uses 'variable bindings' may be more accessible if it just used the term 'variable' instead. The term 'binding' seems uncommon in the documentation of popular languages, whereas 'variable' is fairly ubiquitous. Some people do like to use "binding" for the pattern in which a variable is created, but we don't think the book needs to get that specific. What do you think? Do you think the book would be clearer if it just called them "variables"? |
I've created a pull request so we can review and discuss with context. |
Another detail that came up in #281 has to do with assignments, and I think we could use:
|
I've taken a look at the current text in the book and the comments on this thread, and agree with @eddyb's comment, as does @nikomatsakis. In particular: let x; // declares a variable x, which is not yet bound
let x = 37; // declares a variable x, which is bound to the value 37
const x: i32 = 37; // declares a constant x, whose value is 37 In this usage, "variable" and "constant" both refer to names, but distinguish whether those names will be bound to run-time or compile-time values, respectively. A binding is what attaches one of these names to a value. This is all very standard terminology across a wide range of programming languages, including those that have a similar binding/mutation story to Rust (e.g. Scala). Note that it is the variable not the binding, which is mutable: after all, I agree with the general thrust of this thread that the book will be smoother if we use standard terminology and don't highlight the terminology as much. |
@aturon Well, with What's important though, is that That is, assignment doesn't imply mutation, which I believe matches the difference between other uses of, e.g. "initial assignment" and "reassignment" in English. |
Even if we forgo the "binding" terminology in most of the book, I think it's important to explain it somewhere, as it's used commonly enough in the ecosystem that a new rust user is bound (no pun intended) to come across it. |
I've added a section defining variable binding. After another pass, I think using I think using |
I like using bind and bound, but not before explaining what they mean. I added a section explaining them, but removed it and all uses of bind and bound after @nrc said:
If we use terminology without explaining it, we limit the book's comprehension to those who already know programming languages where the same terminology is used. This is especially bad for users new to programming, or those who've used something like javascript without understanding the technical details. The case could be made that anyone interested in programming has a certain level of intelligence and thus can infer the meanings of bind and bound from the context. But what about non-native English speakers? And is it reasonable to expect the percentage who read the physical book and are obsessed with precise understanding to go searching for an explanation? This could be solved with an appendix and a reference to that, but I would argue that for maximum readability, the terms shouldn't be used before they're defined and the explanation should be inline in the appropriate place. The alternative is only using them in the Patterns chapter, along with an explanation of bindings in the context of pattern leaves, as suggested by @eddyb. |
I don't think "bind" and "bound" are programming terminology. They have a meaning that is common outside of programming:
It's the same thing that's happening here, we tie a name together with a value. |
Instead of talking about variable bindings, would it be better to use the term variables (like other languages) and explain the rest in terms of references?
Let's explore. Are variable bindings:
Variable (mathematics): (from Wikipedia)
In elementary mathematics, a variable is an alphabetic character representing a number, called the value of the variable, which is either arbitrary or not fully specified or unknown.
Variable (computer science): (from Wikipedia)
In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.
Variable: (my definition)
A symbolic name that represents a value or reference.
Niko Matsakis uses the terms variables and references in his into_rust() videos and I'm not confused. Even rustc's error messages use the term variable.
So what is a variable binding and how is it different from a variable? In writing about this, it became clear that after 2 years following Rust, I still don't know. Some possibilities:
Am I missing an aspect to the variable bindings concept?
I think saying a variable is bound to a value or bound to a reference is a great way of expressing it, but does it really mean something different to a variable contains a value or contains a reference?
Does a distinction between variable bindings and variables need to be drawn?
The text was updated successfully, but these errors were encountered: