Skip to content

Json equality for oneOf decoders always returns true #15

Open
@justinmimbs

Description

@justinmimbs

The oneOf decoder stores an Elm list of decoders, but when comparing for equality these lists are compared with _Json_listEquality, which compares JS arrays. The result is that oneOf decoders always compare as equal.


I noticed this when a custom event decoder was not being replaced as I expected. Here's an SSCCE that demonstrates such a case.

module Main exposing (main)

import Browser
import Html exposing (Html)
import Html.Events
import Json.Decode


main : Program () Int Int
main =
    Browser.sandbox
        { init = 0
        , view = view
        , update = always
        }


view : Int -> Html Int
view clicks =
    Html.button
        [ Html.Events.on "click"
            -- good:
            --(Json.Decode.succeed (clicks + 1))
            --
            -- bad:
            (Json.Decode.oneOf [ Json.Decode.succeed (clicks + 1) ])
        ]
        [ Html.text (String.fromInt clicks)
        ]

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