dm3/clojure.java-time

`plus` is not commutative

p-himik opened this issue · 3 comments

(jt/plus  (jt/zoned-date-time) (jt/minutes 1))
=> #inst"2019-04-06T10:35:04.615-00:00"
(jt/plus (jt/minutes 1) (jt/zoned-date-time))
Execution error (ClassCastException) at java-time.amount/d-plus (amount.clj:12).
class java.time.ZonedDateTime cannot be cast to class java.time.Duration (java.time.ZonedDateTime and java.time.Duration are in module java.base of loader 'bootstrap')
dm3 commented

Should plus be commutative though? What do you think should be the laws of algebra involving all of the date-times, intervals and periods?

Currently the API follows the Java object model where the date/time always comes first when a period/duration is added/subtracted. In Java this happens naturally due to the placement of methods. In Clojure we can do as we like. However, date-times and durations are different types - it's not obvious that operations including both should be commutative. Maybe the plus/minus naming could be changed to something like add-duration or extend to clear up the behaviour (although I quite like plus), what do you think?

To be honest, I didn't really give it much thought so I cannot answer your questions properly. It's very well possible that the current state of affairs is the best one. It was just not really intuitive for me (at first, I even started searching in a completely different direction), and I didn't find any previous discussions on the matter.
If all is fine as it is, the issue can be closed and left just as a documentation for someone with a similar concern.

dm3 commented

Added a j/plus API comment.