ecmascript-immutable-data-structures icon indicating copy to clipboard operation
ecmascript-immutable-data-structures copied to clipboard

Some more details about Records.

Open nmn opened this issue 9 years ago • 1 comments

First, I would like to confirm some assumptions about Records. I think they should be added to the .md file for more clarity.

  • records will work with subscripts the same way as objects do
const a = #{a: 1}
a.a === 1
a['a'] === 1
  • Object.assign will work for records as well.
const b = Object.assign(#{}, {a: 1}, {b: 2}, #{b: 3})
b === #{a: 1, b: 3}
  • Values in a record can still be functions and other non-value types.
  • The only way to convert a record to an Object would be to use Object.assign

Some questions:

  • Is there no way of adding or updating a key on a record? With objects you can just set a new value. That seems impossible with records, but potentially very useful.
  • Will other class methods on Object also work on records? Object.keys for example?
  • Object is javascript are currently ordered in all major implementations. Will records also be ordered maps in this way?
  • What is the value of keeping separate types for record and ImmutableMap? Perhaps it's a better idea to combine the two into a single type. It would be called ImmutableMap, but it would actually be an orderedMap, just like javascript Object is practice.

nmn avatar Apr 08 '15 16:04 nmn

I'm not sure if Object.assign should work if a record is the first argument. If it's one of the rest arguments, absolutely, but since Object.assign is expected to mutate the first argument, it seems strange that it would work on an immutable record.

Object.assign(#{}, {a: 1, b: 2}, {c: 3}) // TypeError
Object.assign({}, #{a: 1, b: 2}, {c: 3}) //=> {a: 1, b: 2, c: 3}

ckknight avatar Oct 20 '15 04:10 ckknight