4 ms·
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 defin
by skybrian 10y ago
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.
- kjksf 10y agoIn 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.
- jmgao 10y ago> 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).
- pcwalton 10y ago> 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.