News for hackers. Stuff that matters.
Re:Golang landmines by skybrian on Saturday September 17, 2016 @10:22PM (#12523627)
I'd say the root cause is insisting that each type have a zero value.

Logically, this makes no sense. Types are basically sets. It makes perfect sense to define the set {1,2,3}, without any zero value.

However, if we think of a variable as a memory location, it's very convenient to be able to declare variables without explicitly initializing them.

Re:Golang landmines by kjksf on Sunday September 18, 2016 @12:24AM (#12523926)
In Go, "zero value" is not 0. It's a valid value of a type when the value has not been explicitly provided by a programmer.

In your hypothetical example in Go you would pick one of the set values (e.g. "1") as a "zero value".

The alternative of not having a "zero value" is to have variables with undefined values, and we know from C/C++ that it's extremely bad idea.

Re:Golang landmines by jmgao on Sunday September 18, 2016 @12:58AM (#12524000)
> In your hypothetical example in Go you would pick one of the set values (e.g. "1") as a "zero value".

> The alternative of not having a "zero value" is to have variables with undefined values, and we know from C/C++ that it's extremely bad idea.

Selecting a "zero value" that also happens to have actual meaning is insane.

The real alternative to not having a zero value is to require all variables to be initialized with some value. If you can't, then the variable should be an optional type wrapping the type you want. Pointers/references have something similar (you can have a valid pointer, or you can have null), but optional types are generally applicable to any type, and pulling a value out of an Option generally requires you to check for the null-equivalent or explicitly say "I know what I'm doing, assume it contains something" (unlike null pointers/references, which you can blindly dereference).

Re:Golang landmines by pcwalton on Sunday September 18, 2016 @02:01AM (#12524136)
> The alternative of not having a "zero value" is to have variables with undefined values, and we know from C/C++ that it's extremely bad idea.

No, you can define the language such that zero/undefined values never creep into the semantics. MLs traditionally do this. I believe Swift does this.