Fix #268 by introducing a MaybeN type #425
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Taking a stab at fixing #268 by introducing a new
MaybeN
type, which is just likeMaybe
except thatNothing
values get encoded to JSONnull
. This is inspired by the existingList
type.This can be used to conform properly to the LSP spec, which sometimes specifies types like
a | null
and sometimesa?
(i.e. omit the value when absent).The downside of this approach is that for every field you convert from
Maybe
toMaybeN
, you have to go through all usages of that field and change them. I've included an example of doing this forInitializeParams
; for full compliance we would have to do the same for allMaybe
fields that are supposed to bea | null
.Of course, it's unpleasant to expose this encoding detail in the field types and make all these changes. This seemed like the most straightforward way, but a cleaner approach might be possible with TH. For example, maybe we could extend
makeExtendingDataType
so that it takes extra arguments indicating which type ofMaybe
to use, and generates suitableFromJSON
/ToJSON
instances manually? FWIW a search through the LSP spec indicates about 40 cases of| null
to handle.