Open rbuckton opened 4 years ago
Rather than introducing weird overload for .
operator, which accesses property, wouldn't it be better to overload +
for objects/maps/sets/arrays?
This would lead to intuitive code:
obj + { a: 3, c: 4 }
obj = obj + { a: 3, c: 4 }
obj += { a: 3, c: 4 }
@stiff no, that’s not possible, since it already has existing semantics. A new operator would be required.
New is ok, reusing existing with pretty much opposite function to previous is not.
By "new" i mean "was a syntax error previously". .{
is new, +
with objects is not.
since it already has existing semantics.
@ljharb I think overloading +
for tuple/record is possible, just like how we overload +
for bigints. So #[1, 2] + #[3, 4] === #[1,2,3,4]
, #{a:1} + #{b:2} === #{a:1, b:2}
could work. I'm not sure whether such overloading is good or bad, but I guess that we'd better throw TypeError for any +
operation when operand is tuple/record for now, so we keep the overloading possibility in the future.
There was a strawperson many years ago for adding a
.{
("monocle mustache") operator for assigning multiple properties to an object that might be worth investigating. IIRC, the basic idea was this:This might be a useful operator to consider for replacing deep properties in records/tuples as opposed to, or in addition to, spread (
...
):The question would be whether
.{
on a regular object would be mutating even if.{
on a record/tuple would not.