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

Implement PEP 448 Additional Unpacking Generalizations #1128

Closed
gilch opened this issue Oct 2, 2016 · 6 comments
Closed

Implement PEP 448 Additional Unpacking Generalizations #1128

gilch opened this issue Oct 2, 2016 · 6 comments
Labels

Comments

@gilch
Copy link
Member

gilch commented Oct 2, 2016

See PEP 448 for what I'm talking about. See also #891.

@gilch
Copy link
Member Author

gilch commented Nov 9, 2016

Hy would need its own syntax for this kind of thing. We are already used to doing similar things with reader macros and syntax quotes, ~@ is very similar to Python's * when you think about it.

I propose #* and #:, for example:

;; hy --spy
=> [1 #*[2 3 4] 5 6 #*[7 8 9]]
[1, *[2, 3, 4], 5, 6, *[7, 8, 9]]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
=> {#:{1 10} #:{2 20} 3 30} 
{**{1: 10}, **{2: 20}, 3: 30}
{1: 10, 2: 20, 3: 30}

The # makes it clear that this is reader syntax, and * because that's how Python does it. The : has two dots like Python's equivalent **, and also reminds you of Python's {:} syntax and Hy's :keywords. But I suppose we could use #** instead. We don't have to restrict reader macros to one character. #1046.

@Kodiologist
Copy link
Member

I'm wondering if I should work on this, so we can get rid of apply, rather than fixing #647. Personally, I like the :* and :** syntax you introduced first rather than #-prefixed symbols, but I guess it shouldn't make much of a difference.

@gilch
Copy link
Member Author

gilch commented Jun 22, 2017

An improved apply form could use :* and :** as kwargs. (This could even be done as a macro in terms of the current apply.), but using that syntax for all unpacking would be inconsistent, since it blocks what would otherwise be valid kwarg names in function calls. They would also only work once each per form, since a kwarg can only take one value per call.

The unpacking generalizations, on the other hand, can be used multiple times per form. Like quote/' in most lisps, they could be special forms with a macro shortcut. It has to be #-prefixed, unless we want to use up another dispatch character.

@Kodiologist
Copy link
Member

No, I mean making :* and :** magic, so you can write (f :* a :* b) or whatever. It doesn't require any changes to the reader; it's just another special case for how to interpret literal keywords when they appear in a function call. * and ** are crummy names for keyword arguments, so no loss there.

@gilch
Copy link
Member Author

gilch commented Jun 22, 2017

The Python unpacking generalizations can appear not just in function calls, but in tuple, list, set, and dictionary displays. (And they may also appear in comprehensions in the future.) So Hy would also need them in the equivalent special forms, but keywords aren't magic there--they're data. It seems a lot more consistent to me to use #-something. You wouldn't have to add so many exceptions and special cases.

I'm also not certain how #1127 should look, but it seems like a similar idea, so it would be nice if they had common syntax.

@Kodiologist
Copy link
Member

Good point. #* and #** it is, I guess.

I'm also not certain how #1127 should look, but it seems like a similar idea, so it would be nice if they had common syntax.

I assumed you'd use the identical syntax, like (setv [a #* b c] x) for a, *b, c = x.

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

No branches or pull requests

2 participants