Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think there are lots of good reasons to make datetimes immutable (e.g. many of the same reasons most languages make strings immutable). But here's one specific to Moment and Luxon: chainable APIs should really always have immutable types because the return value is the thing you use next anyway. When each call returns an instance of the type it's both completely unnecessary and very often harmful for those calls to modify anything.

  var a = moment();
  console.log("the end of tomorrow is", a.add(1, 'day').endOf('day'));

  // do more stuff with a
  // oh crap, a is now set to tomorrow? wtf?
If you want a to be the end of tomorrow, all you needed to do was assign a to the whole chain, so this mutation didn't help you. That add() and endOf() return instances of Moment signals that they are Moment -> Moment functions, not mutators that also return the object. A mutable API should have void methods:

  var a = moment();
  a.add(1, 'day');
  a.endOf('day');

  // do stuff with a


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: