News for hackers. Stuff that matters.
Re:Nil in Go is typed in theory and sort of untyped in practice by stcredzero on Wednesday March 31, 2021 @01:06PM (#26649883)
0 also is untyped, which is why you can write x==0 regardless of whether x is an int32 or an int64...

Since nil does not have a default type, it's an error to write "x := nil".

But not an error to write "x := 0"?

Re:Nil in Go is typed in theory and sort of untyped in practice by dhagz on Wednesday March 31, 2021 @01:21PM (#26650108)
Because 0 has a "default type" of int. So when the compiler goes to determine the type of x, it sees its set to 0, which the compiler assumes to be an int.

I've found thinking about nil too much in Go hurts or creates weird loops in my brain, so I just tend to worry about it when I'm dealing with interfaces or pointers. Everything else I'm looking at zero-values.

Re:Nil in Go is typed in theory and sort of untyped in practice by stcredzero on Wednesday March 31, 2021 @02:01PM (#26650713)
I'd much prefer it if in golang, the type of nil was some "nilType" for which != and == is defined for all other types. Or, define some other operation, like isNil()

Then determining if something is nil could be done without thinking about it at all. nil would be nil would be nil. (Also, it would be illegal to put methods on nilType, of course.)

(Yes, I make my living writing go, and this would make my life easier. And no, in that case, don't allow x := nil.)

Re:Nil in Go is typed in theory and sort of untyped in practice by masklinn on Wednesday March 31, 2021 @01:29PM (#26650219)
No, because as they note constants can have a default type[0]. For integer constants, that's `int`. Meaning if a literal integer constant is not otherwise typed, it will default to `int`.

[0] https://blog.golang.org/constants#TOC_5.

Re:Nil in Go is typed in theory and sort of untyped in practice by saghm on Wednesday March 31, 2021 @01:18PM (#26650074)
The next paragraph they wrote explains this: "Unlike 0 or "", nil does not have a default type. The default type of a constant is the type that it is implicitly converted to when a type is required. For example, "x := 0" declares a new value x with the value 0, but 0 is untyped. Untyped integer constants have a default type of int, so x is given the type int."