Skip to content

Should Maybe codec return Nothing for nulls? #8

Closed
@garyb

Description

@garyb

If you're writing a codec for a value that cannot have a DecodeJson instance declared for it, it seems sensible to write something like this:

traverse decodeCustom =<< obj .? "prop"

To get a Maybe Custom success result. The idea being obj .? "prop" result is a Maybe Json and then decodeCustom does the Json -> Custom step. However, the decodeJsonJson instance always succeeds, as null is a decodeable value, so you end up with Just null as the response, and then decodeCustom fails as it's probably expecting something else.

If you're using instances everywhere this works fine, as the custom decoder would run "inside" the Maybe decoder, and failing there gives you a Nothing result.

Maybe it was a faulty assumption on my part that null values would be considered Nothing? It took me a really long time to track this down though. 😭

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions