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
spec: nil, true, false not reserved words #18193
Comments
This is intentional. They are predefined identifiers:
https://golang.org/ref/spec#Predeclared_identifiers
One of the reason is that we can extend the language
by introducing more of these without breaking existing
program that already make use of the extended identifier
as a user identifier.
Please ask questions on other mediums, see
https://golang.org/wiki/Questions.
|
@minux: Making "nil" a reserved word does not prevent the introduction of future identifiers that are not called "nil", so the explanation doesn't make sense. Certainly, if you were to introduce a new special value today called "foo", for example, you simply couldn't avoid having to make it identifier instead of a reserved word. But "nil" and friends have existed since before the dawn of Go. Besides, does assigning new values to Go is very strict about things like unused imports and unused variables, yet it curiously has no problem with this. At the very least |
The explanation does apply to "append", which is introduced later.
One other reason is that a lot of the predeclared identifiers makes
perfect sense to be used as a user defined name sometimes (within
in a small scope, perhaps).
For example, naming two variables old and new is quite reasonable.
go vet shouldn't out law such uses.
I think this question has been discussed extensively in the past,
please search the mailing list archives.
|
@atombender Making |
Of course. However, that does not explain the rationale for why it was designed like this in the first place.
… On Dec 5, 2016, at 01:16, Ian Lance Taylor ***@***.***> wrote:
@atombender Making nil a reserved word would break existing correct Go programs, and would therefore violate the Go 1 guarantee (https://golang.org/doc/go1compat).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@atombender The rationale is pretty simple: Only identifiers that must be keywords for syntactic reasons are keywords. In fact, in the very beginning there was some discussion as to whether things like nil, iota, etc. should be keywords. Eventually we agreed on the rule above which settled it. |
I'll add that once the language has this concept, it becomes easy to add new predeclared identifiers without breaking existing programs. |
That's been clearly stated by now, but seeing as the |
@griesemer: I suggest that the documentation is updated to include the explanation, because it's not obvious to someone who wasn't there. The rationale is unusually unpragmatic compared to Go's other design choices, and also unusually lax for Go, which is otherwise terribly strict and opinioned about so many things. |
@simen It's not impossible to grasp, it's just not been done... :-) And the reason is probably that it's not important to check because it just doesn't happen to be an issue. There's simply more important things to worry about. Also, it's a different issue from this one, so please don't hijack this issue for another subject. This issue is closed. Thanks. @atombender Feel free to submit a CL for review (perhaps for the FAQ). Also, I respectfully disagree. The rationale is very pragmatic: it's a simple rule that makes it easy to decide what needs to be a keyword and what doesn't; and as @ianlancetaylor has pointed out, it makes it easy to add new predeclared identifiers. Finally, Go is in no way unusual about where it is strict and where not as far as statically typed languages go. The approach chosen for keywords and predeclared identifiers is not new either and has a long history going back at least 40 years of programming language design (Pascal). |
@griesemer But other languages are more strict than Go here; 40 years of programming language design has provided us with unassignable constants for these things. You can't redefine or reassign I'll submit a PR if I have time. |
A PR isn't the problem. This was a design decision. |
@atombender I'm not surprised about C++... But at least the Python I checked doesn't do anything special:
I'm not saying there's isn't languages where this is not the case. But Go is not the only language that made the respective decision. There's also a question about the wisdom of making these restrictions: It doesn't really prevent people from writing bad code. I could still write |
A FAQ entry may be ok explaining this was simply deliberate and carefully thought out design decision - but there's no need to debate this any further. I'm going to lock this issue. |
@bradfitz I meant PR for the FAQ/docs. @griesemer Python isn't strict about the booleans (which are just values), but is about |
@atombender Thanks for the clarification. We did discuss making nil a reserved word, but the regularity of the rule mentioned above trumped the irregularity for nil. (The reason was - if I remember correctly - that if one changed nil one couldn't recreate it from whole cloth. On the other hand, if somebody at the top-level changed true and false, one would always be able to re-create them, say via |
What version of Go are you using (
go version
)?go1.7.3 darwin/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/5dRCQ7TYEB
What did you expect to see?
Expected the code not to compile because it assigns arbitrary values to (what I expected to be) important reserved words.
What did you see instead?
The code works as … unexpected and prints
"Hello, World"
.I understand these have status as Predeclared identifiers (https://golang.org/ref/spec#Predeclared_identifiers) but question the value of not flagging these as errors immediately.
The text was updated successfully, but these errors were encountered: