elm / core

Elm's core libraries

Home Page:http://package.elm-lang.org/packages/elm/core/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

equality is not commutative for Json.Encode.Value

BrianHicks opened this issue · comments

I assume this happens for Json.Decode.Value as well!

Here is the SSCCE on Ellie (contents reproduced below.)

module Main exposing (main)

import Html exposing (text)
import Json.Encode exposing (Value, int, object)


leaf : Value
leaf =
    int 1


value1 : Value
value1 =
    object
        [ ( "a", leaf )
        , ( "b", leaf )
        ]


value2 : Value
value2 =
    object
        [ ( "a", leaf )
        ]


main =
    Html.div []
        [ Html.text (Debug.toString (value2 == value1))
        , Html.text (Debug.toString (value1 == value2))
        ]

I expected this to print "FalseFalse", but it prints "TrueFalse".

Equality is not supported for Json.Encode.Values for a variety of reasons. The docs say:

Problematic types include functions and JavaScript values like Json.Encode.Value which could contain functions if passed through a port.

But further problems can come from values coming in through ports. E.g. You may have self-referential objects. If two objects are "structurally equal" but are constructed such that their self-referential aspects refer to different objects, how should equality be computed?

So in the long run, this should be a type error. For now, it is documented that it does not work right.