Comment by Mawr

1 day ago

For JSON I just marshall to/from:

    {
     "value": "value",
     "exists: true,
    }

For nil, that's interesting. I've never ran into issues there, so I never considered it.

Ideally, I would want Optional[T] to encode the same as T when a value is present, and to encode in a configurable way when the value is absent. Admittedly, the nothing to null problem exists with *T too, and even with *T and `json:",omitempty"`, you get the opposite problem (null turns to nothing). I didn't think about that at the time, so it's really more of an issue with encoding/json rather than Optional[T] per se. However, you can't implement MarshalJSON and output nothing as far as I know.