Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring #11

Open
lantiga opened this issue Jun 21, 2014 · 6 comments
Open

Destructuring #11

lantiga opened this issue Jun 21, 2014 · 6 comments

Comments

@lantiga
Copy link
Owner

lantiga commented Jun 21, 2014

Consider introducing destructuring.

@fasterthanlime
Copy link
Contributor

I'm very interested in this feature. How complex do you think it would be to implement in terms of sweet.js macros/rules?

@lantiga
Copy link
Owner Author

lantiga commented Nov 4, 2014

Got this

(let [[a [b] c [[d e] f]] [1 [2] 3 [[4 5] 6]]]
  (add a b c d e f))
=> 21

working with this 5fd812b commit (works only for nested vector in a let form). It's a start, I'm quite impressed at how little code it took with sweet.js.

@lantiga
Copy link
Owner Author

lantiga commented Nov 4, 2014

And here's fe371c0 destructuring of maps (mixed with vectors) in let forms:

(let [{[a] :a b :b} {:a [1] :b 2}]
     [a b])
=> [1 2]

Well, that does it (for mori data structures and literals - we could probably consider adding destructuring with js array and object literals [$ ] {$ }, we'll see about it).

Now I have to figure out how to call the _destr macro in

  • function signatures (single and multiple arity)
  • loop forms
  • letc (this one has some kind of hard-coded destructuring in a list, it could actually be left alone since it's the only destructuring that should be supported here)

Anywhere else?

@fasterthanlime
Copy link
Contributor

From [http://clojure.org/special_forms#Special Forms--Binding Forms (Destructuring)](http://clojure.org/special_forms#Special Forms--Binding Forms %28Destructuring%29):

Clojure supports abstract structural binding, often called destructuring, in let binding lists, fn parameter lists, and any macro that expands into a let or fn.

So I think your list had it covered. I have mixed feelings about letc, it's probably really useful for Node.js-style APIs, but does it belong in core? Not that it's expensive to have it there, though.

Destructuring JS arrays/objects sounds interesting (and would actually give ki an edge over vanilla cljs), but probably not high-priority for now :)

@lantiga
Copy link
Owner Author

lantiga commented Nov 4, 2014

Great, thanks for the quote - somehow it didn't occur to me that I could check on the Clojure website.

I feel the same way as you do about letc. It's possibly a nice to have, but it could indeed belong to an external macro library if we have enough of them at some point.

As for the JS arrays/objects destructuring, I just added it: 6ac7006

@lantiga
Copy link
Owner Author

lantiga commented Nov 4, 2014

Added destructuring in loop 16b3051 and tests for destructuring so far 06760fa.

Adding destructuring to function signatures is harder due to the way sweet tokenizes the arguments. I had a working prototype for 1-arity functions but the thing brake down for n-arity. Needs some more thinking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants