3 ms·
> Though I don't really see how "int*" is different from "Optional<int>". One requires you to handle the possibly-nil case in order to access the pointer, the
by stouset 6y ago
> Though I don't really see how "int*" is different from "Optional<int>".
One requires you to handle the possibly-nil case in order to access the pointer, the other expects you to remember to check every time.
The following is completely legal, and results in a runtime error:
func foo(maybeInt *int) {
x := *maybeInt // runtime panic
}
The following is not, and results in a compile-time error:
func foo(maybeInt Optional<int>) {
x := *maybeInt // compile error: need to unwrap maybeInt
}
- zimpenfish 6y agoAnnoying that the various Go linters I have installed (`go vet`, `golangci-lint`, `gosec`) don't catch "possible use of pointer before nil-check" - you'd think it would be an obvious case for those to handle.
- masklinn 6y agoThat would be very annoying to use as many pointers are just assumed to be non-nil and you don't have any other option but to assume they're non-nil. Also since Go supports nil receivers, it would have to require that every "pointer method" be checked for nil. Which technically they should, but...
- zimpenfish 6y agoI'd imagine it'd be one of the "disabled by default" in `golangci-lint`, for example - helpful for when you're doing an in-depth review of the code but probably not something you want running on every CI invocation.