Comment

Comment by Mawr

  type Optional[T any] struct {
    value *T
  }
Don't use a pointer - it's bad for performance.

  func MakeOptional[T any](value *T) Optional[T] {
Use `New` instead - it's the idiomatic name for a constructor in Go. Drop the `Optional` part - the module name suffices - the caller will see: `opt.New`. Personally I just call the module `optional`, I think it's clearer: `optional.New`.

  func (o* Optional[T]) Unwrap() (T, error) { 
1. `Unwrap` reminds me of Rust's .Unwrap, which panics. Seems confusing.

2. There's no error here, the situation is equivalent to a missing key in a map - it's enough to return a bool.

Here's my implementation so far: https://gist.github.com/Mawr-BF2/0a60da26f66b82ee87b98b03336.... Only thing I'm not sure about is whether the JSON serialization methods are necessary.

parent Mawr 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton