1

With syntax highlighting enabled, it's distracting while reading code like answer to this question with new used as a variable name.

I'm trying to think of a reason why only a subset of keywords would be reserved and can't come up with a good one.

Edit: Alternate title for this question:

Why are Go's predeclared identifiers not reserved ?

Community
  • 1
  • 1
Sridhar
  • 2,416
  • 1
  • 26
  • 35

2 Answers2

4

That's because new and make aren't really keywords, but built-in functions.

If you examine the full list of the reserved keywords, you won't see len or cap either...

Elwinar
  • 9,103
  • 32
  • 40
  • This may be. But "Package builtin provides documentation for Go's predeclared identifiers. The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language's special identifiers." – Sridhar Aug 13 '15 at 12:13
  • Perhaps the question should be re-titled "Why are Go's predeclared identifiers not reserved?". – Sridhar Aug 13 '15 at 12:16
  • The point is that identifier can be redefined by shadowing. I never tried it, but I'm confident you cannot define a `func len` but that you can define a `var len`. – Elwinar Aug 13 '15 at 12:18
  • I was wrong, turns out that you actually *can* define a `func len`. See http://play.golang.org/p/-gqQVDkvXY – Elwinar Aug 13 '15 at 12:19
  • I initially thought maybe you're able to do that because your len() has no arguments. But can also shadow len(s []byte). See http://play.golang.org/p/CZztd0g6s7 – Sridhar Aug 13 '15 at 12:26
  • There is no overloading in Go, so my method effectively override the builtin `len` method. See http://play.golang.org/p/X_7YBw4nw1 – Elwinar Aug 13 '15 at 12:31
  • Sorry don't get it - what's that supposed to exhibit. It doesn't run. – Sridhar Aug 13 '15 at 12:35
  • That's the point: when you define a function named `len`, the builtin function isn't accessible anymore. As a generality, I won't use any predefined name in my code, excepted `new` which builtin I never used. – Elwinar Aug 13 '15 at 12:37
  • Ok I get it. Good one. – Sridhar Aug 13 '15 at 12:38
2

In short: because using predeclared identifiers in your own declarations don't make your code ambiguous.


new and make are builtin functions, amongst many others. See the full list in the doc of the builtin package.

Keywords are a carefully chosen small set of reserved words. You cannot use keywords as identifiers because that could make interpreting your source ambiguous.

Builtin functions are special functions: you can use them without any imports, and they may have varying parameter and return list. You are allowed to declare functions or variables with the names of the builtin functions because your code will still remain unambiguous: your identifier in scope will "win".

If you would use a keyword as an identifier, you couldn't reach your entity because an attempt to refer to it by its name would always mean the keyword and not your identifier.

See this dirty example of using the predeclared identifiers of the builtin function make and the builtin type int (not recommended for production code) (try it on the Go Playground):

make := func() string {
    return "hijacked"
}

int := make()    // Completely OK, variable 'int' will be a string
fmt.Println(int) // Prints "hijacked"

If you could use keywords as identifiers, even interpreting declarations would cause headache to the compiler: what would the following mean?

  • func() - is it calling your function named func or is it a function type with no params and no return types?
  • import() - is it a grouped import declaration (and importing nothing) or calling our function named import?
  • ...
icza
  • 389,944
  • 63
  • 907
  • 827