Skip to content
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

proposal: Go 2: spec: expand reserved keywords list to include: true, false, nil #25490

Closed
andizzle opened this issue May 22, 2018 · 8 comments
Closed
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@andizzle
Copy link

andizzle commented May 22, 2018

Should this be allowed? https://play.golang.org/p/_ige_iHPQEn

false := true
nil := false

fmt.Println(false, nil)
@andizzle andizzle changed the title expanding reserved keywords list? expand reserved keywords list? May 22, 2018
@meirf
Copy link
Contributor

meirf commented May 22, 2018

That would break Go1 code so this would probably need to wait for Go2, pending approval.

@odeke-em odeke-em changed the title expand reserved keywords list? spec: expand reserved keywords list to include: true, false, nil May 22, 2018
@odeke-em
Copy link
Member

@andizzle, true, false are predeclared constants, nil, iota are predeclared identifiers whose values can be created even by programmers so am not sure what the bar for reserving them would and perhaps that might also warrant reserving predeclared types? As @meirf notes this would break current code.

I'll loop in @griesemer for his wisdom

@dsnet dsnet changed the title spec: expand reserved keywords list to include: true, false, nil proposal: spec: expand reserved keywords list to include: true, false, nil May 22, 2018
@gopherbot gopherbot added this to the Proposal milestone May 22, 2018
@dsnet dsnet added LanguageChange v2 A language change or incompatible library change and removed Proposal labels May 22, 2018
@dsnet dsnet changed the title proposal: spec: expand reserved keywords list to include: true, false, nil proposal: Go 2: spec: expand reserved keywords list to include: true, false, nil May 22, 2018
@ALTree
Copy link
Member

ALTree commented May 22, 2018

This is a dup of #18193; which includes an explanation for this design decision.

@griesemer
Copy link
Contributor

If anything, we want to remove keywords if at all possible. Again, see #18193 for the details of the discussion.

If you want to write false := true, go right ahead. There's plenty of ways one can shoot oneself in the foot in any language, and that's also true for Go. The solution is simple: Don't do that!

I'm going to close this as there's zero chance of this getting adopted. It also doesn't solve any real problem.

@andizzle
Copy link
Author

@griesemer just want to point out the Python example given in #18193, is no longer the case in Python 3.

Python 3.6.5 (default, Mar 30 2018, 06:41:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> False = True
  File "<stdin>", line 1
SyntaxError: can't assign to keyword

Thanks for point out that this is a design decision, that's all I'm looking for.

@griesemer
Copy link
Contributor

@andizzle Thanks for the update. The question really is: Is it worthwhile to add more keywords, which then have to be mentioned in the respective expression grammar, and which have to be explained in the spec, simply to protect against people doing false := true? By making true, false, and nil predeclared constants (or value, for nil), no extra keywords need to be introduced, the expression grammar doesn't need special cases, and the usual scoping rules take care of the rest. It's simpler and gets the job done w/o exceptions.

Or looking from a different angle: If we were to disallow false := true (which could also be done with special handling of those predeclared identifiers, not just with keywords), should we also prevent zero := 1.0 ? And if not, why not? How is it different? (Surely, anyone seeing zero would assume it to be some sort of zero value!). Or what about disallowing the declaration of a local function panic that doesn't panic? Etc.

There's plenty of opportunity for mischief in Go (and any language for that matter) - and these examples are things people don't really do accidentally in the real world. They do these if they want to prove a point or, well, create trouble (for themselves or others). We can't prevent this and thus we don't even try in these cases.

Thanks.

@griesemer
Copy link
Contributor

PS: There's a different design argument that could be made if true, false, and nil were the only predeclared identifiers: Maybe then it would make sense to make them keywords, and then there wouldn't be any notion of predeclared identifiers. But Go has many predeclared identifiers, so adding these three fits right into the design w/o complicating it.

@andizzle
Copy link
Author

andizzle commented May 22, 2018

Agree. If people want to write false := true, let them. To me, this question raised because I naturally assumed this behaviour would be considered as abnormal, or somehow should be illegal because it "looks wrong". But I now see the reason behind this design decision.

This is unlike some cases in other languages where if you don't know about them, you can be tricked. If people deliberately write false := true, then they are looking for trouble.

Thanks again for clarifying this.

@golang golang locked and limited conversation to collaborators May 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants