3 ms·
That still has the same issues depending on the usage. The default construct here is `NotificationWrapper{nil}`. If you want to actually guard it, you also need
by flakes 2y ago
That still has the same issues depending on the usage. The default construct here is `NotificationWrapper{nil}`. If you want to actually guard it, you also need to make the Value field private as that nil becomes part of your public api otherwise.
type NotificationWrapper struct {
// This field holds the actual notification data
value interface{}
}
Within `ProcessNotification` you'd also want to always assert the struct is initialized. You have to handle that error case via `err` or `panic`.
With a true sum type, you could be assured that the value is always concrete, removing the need for error handling, which otherwise complicates the business logic of your application. Errors that should be safeguarded against by the compiler become run time checks, eating up cycles on the CPU, or paniced upon, potentially leading to segfaults which can only be caught by tests (not asserted valid via the act of compiling).