jashkenas/coffee-script — Issue #1864: Object interpolation
Author: gravof
Created: 2011-11-13 11:02:19+00:00
State: open

Can we support this kind of syntax:

obj =
b: 2
{
a: 1
obj
c: 2
}

which will be compiled into something like this:

var _extend = function () { //... }
var obj = { b: 2 };
_extend({ a:1 }, obj, { c:2 });

Nested version:

obj1 =
b: 2
obj2 =
e: 5
{
a: 1
obj1
c:
d: 4
obj2
}

which will be compiled into:

var _extend = function () { //... }
var obj = { b: 2 };
_extend({ a:1 }, obj, { c: _extend({d: 4}, obj2) });

`obj` doesn't have to be a variable, it can also be a literal (array, object, etc). Is this idea feasible?

I see CS currently compiles this

fn
a:1
obj
b:2

into this

fn({
a: 1
}, obj, {
b: 2
});

I'm not sure if it's OK to break this compatibility, but I think it would be really cool if we could "interpolate" into object literals