olleicua/hcl

Add destructuring bind

Closed this issue · 2 comments

I'm thinking the best way to do this would be to add a form to set (and set+ etc). Something like:

HCL:

(set (a b c) [1 2 3])

(var obj {})
(def foo (# () ["foo" "bar"]))
(set (obj.foo obj.bar) (foo))
;; obj is now {foo "foo" bar "bar"}

JavaScript:

((a=1),(b=2),(c=3),[1,2,3]);

var obj = {};
var foo = function() { return ["foo", "bar"]; };
(function(_array_){ obj.foo = _array_[0]; obj.bar = _array_[1]; return _array_; }).call(this, foo());

It may also be useful to have a destructuring form of let. Perhaps it can be as simple as if an even indexed element of the first argument of the let is a list then it destructures so that

(let (a 1
      (b c) [2 3])
   (console.log a b c))

produces 1 2 3

I'm considering allowing splats in destructurings but I'm not sure if it would really be worth it to add the full object destructuring from coffeescript.

see coffeescript destructuring