Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upValue comparison does not work for Date.Date #468
Comments
lukewestby
changed the title from
Value comparison does not work for `Date.Date`
to
Value comparison does not work for Date.Date
Dec 29, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
athanclark
May 3, 2016
I'm not sure if this is related, but I've needed to compare Dates and have been disappointed that they're not natively comparable. I had to build this verbose utility function for comparing months, may you scrape it for success!
compareMonth : Month -> Month -> Order
compareMonth m1 m2 =
case (m1, m2) of
(Jan, Jan) -> EQ
(Jan, _) -> LT
(Feb, Jan) -> GT
(Feb, Feb) -> EQ
(Feb, _) -> LT
(Mar, Jan) -> GT
(Mar, Feb) -> GT
(Mar, Mar) -> EQ
(Mar, _) -> LT
(Apr, Jan) -> GT
(Apr, Feb) -> GT
(Apr, Mar) -> GT
(Apr, Apr) -> EQ
(Apr, _) -> LT
(May, Jan) -> GT
(May, Feb) -> GT
(May, Mar) -> GT
(May, Apr) -> GT
(May, May) -> EQ
(May, _) -> LT
(Jun, Jan) -> GT
(Jun, Feb) -> GT
(Jun, Mar) -> GT
(Jun, Apr) -> GT
(Jun, May) -> GT
(Jun, Jun) -> EQ
(Jun, _) -> EQ
(Jul, Dec) -> LT
(Jul, Nov) -> LT
(Jul, Oct) -> LT
(Jul, Sep) -> LT
(Jul, Aug) -> LT
(Jul, Jul) -> EQ
(Jul, _) -> GT
(Aug, Dec) -> LT
(Aug, Nov) -> LT
(Aug, Oct) -> LT
(Aug, Sep) -> LT
(Aug, Aug) -> EQ
(Aug, _) -> GT
(Sep, Dec) -> LT
(Sep, Nov) -> LT
(Sep, Oct) -> LT
(Sep, Sep) -> EQ
(Sep, _) -> GT
(Oct, Dec) -> LT
(Oct, Nov) -> LT
(Oct, Oct) -> EQ
(Oct, _) -> GT
(Nov, Dec) -> LT
(Nov, Nov) -> EQ
(Nov, _) -> GT
(Dec, Dec) -> EQ
(Dec, _) -> GT
athanclark
commented
May 3, 2016
|
I'm not sure if this is related, but I've needed to compare compareMonth : Month -> Month -> Order
compareMonth m1 m2 =
case (m1, m2) of
(Jan, Jan) -> EQ
(Jan, _) -> LT
(Feb, Jan) -> GT
(Feb, Feb) -> EQ
(Feb, _) -> LT
(Mar, Jan) -> GT
(Mar, Feb) -> GT
(Mar, Mar) -> EQ
(Mar, _) -> LT
(Apr, Jan) -> GT
(Apr, Feb) -> GT
(Apr, Mar) -> GT
(Apr, Apr) -> EQ
(Apr, _) -> LT
(May, Jan) -> GT
(May, Feb) -> GT
(May, Mar) -> GT
(May, Apr) -> GT
(May, May) -> EQ
(May, _) -> LT
(Jun, Jan) -> GT
(Jun, Feb) -> GT
(Jun, Mar) -> GT
(Jun, Apr) -> GT
(Jun, May) -> GT
(Jun, Jun) -> EQ
(Jun, _) -> EQ
(Jul, Dec) -> LT
(Jul, Nov) -> LT
(Jul, Oct) -> LT
(Jul, Sep) -> LT
(Jul, Aug) -> LT
(Jul, Jul) -> EQ
(Jul, _) -> GT
(Aug, Dec) -> LT
(Aug, Nov) -> LT
(Aug, Oct) -> LT
(Aug, Sep) -> LT
(Aug, Aug) -> EQ
(Aug, _) -> GT
(Sep, Dec) -> LT
(Sep, Nov) -> LT
(Sep, Oct) -> LT
(Sep, Sep) -> EQ
(Sep, _) -> GT
(Oct, Dec) -> LT
(Oct, Nov) -> LT
(Oct, Oct) -> EQ
(Oct, _) -> GT
(Nov, Dec) -> LT
(Nov, Nov) -> EQ
(Nov, _) -> GT
(Dec, Dec) -> EQ
(Dec, _) -> GT |
evancz
closed this
in
7eb27a1
Jun 25, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lukewestby commentedDec 29, 2015
Simplest reproducible example, doable in the REPL:
This is happening because instances of JS
Dateare "exotic" objects and not primitives. They have no own properties and end up falling through both the strict equality and object cases forUtils.eq(). They're a rather strange edge case as far as JS object comparison goes, as they need to be coerced to theirNumberrepresentation first with+orvalueOf()first.The workaround for now that I've been using is to do the same sort of coercion, just in Elm:
But IMVHO it seems like if
Dateis going to be a built-in data type for the language, it should be possible to compare instances reliably without transformation.The way I would plan to handle this is by adding a top-level case to😄
eqto checkx instanceof Date && y instanceof Dateand then comparex.valueOf() === y.valueOf(). If that plan makes sense to you I can submit a PRThanks!