Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



> 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).


> 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: