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

Variadic (or "Varargs") destructuring of tuples #9870

Closed
habemus-papadum opened this issue Jan 21, 2015 · 11 comments
Closed

Variadic (or "Varargs") destructuring of tuples #9870

habemus-papadum opened this issue Jan 21, 2015 · 11 comments
Labels
speculative Whether the change will be implemented is speculative

Comments

@habemus-papadum
Copy link

In julia, is there some syntax similar to
(x,z...) = (1,2,3) which would result in
x=1 & z=(2,3)?

If not, are there reasons why this was not supported, or perhaps this could be a reasonable feature request (or, more constructively, prior to working on a PR, do others have thoughts about this)?

Ideally, since nested destructuring e.g. (a,(b,c)) = (1,(2,3)) works as expected, any variadic version would also work e.g.
(a,(b,c,d...),e...) = (1,(2,3,4,5),6,7) would yield

a=1
b=2
c=3
d=(4,5)
e=(6,7)

And perhaps

(a,(b,c,d...),e..., nexttolast, last) = (1,(2,3,4,5),6,7,8,9,10) would yield

a=1
b=2
c=3
d=(4,5)
e=(6,7,8)
nexttolast=9
last=10

While (a,(b,c,d...),e..., nexttolast, last) = (1,(2,3,4,5),9,10) would yield

a=1
b=2
c=3
d=(4,5)
e=()
nexttolast=9
last=10

And finally (a,(b,c,d...),e..., nexttolast, last) = (1,(2,3,4,5),10) would result in an error, just
like the current behavior for (a,b) = 1

@mschauer
Copy link
Contributor

The package https://github.com/kmsquire/Match.jl allows to do this

> a, b, c = @match (1,2,(3,)) (a,b, (c,))
(1, 2, 3)

@jiahao
Copy link
Member

jiahao commented Jan 21, 2015

This is a usage question for the julia-users mailing list. The issue tracker is for bug reports.

@jiahao jiahao closed this as completed Jan 21, 2015
@timholy
Copy link
Member

timholy commented Jan 21, 2015

You could interpret this as a feature request for such syntax.

@lilinjn, tuplesplit(x) = x[1], tuple(x[2:end]...) should get you started. To me that looks like attractive syntax, so if you can figure out how to add it to the parser, a PR might be nice. (Just my opinion.)

@StefanKarpinski
Copy link
Member

@jiahao, the topmost syntax doesn't work:

julia> (x,z...) = (1,2,3)
ERROR: syntax: invalid assignment location

I'm pretty sure I've proposed this before and I think we can reasonably view this issue as a feature request for this syntax.

@StefanKarpinski StefanKarpinski added feature speculative Whether the change will be implemented is speculative labels Jan 21, 2015
@ivarne
Copy link
Member

ivarne commented Jan 21, 2015

Implementing this can also prepare the way for deprecating the sometimes surprising behaviour where a, b = 1,2,3 discards the third value.

@StefanKarpinski
Copy link
Member

I'm torn about discarding trailing values. It makes it possible to have silent bugs where you drop values that you didn't mean to, but on the other hand, it allows gracefully extending APIs by returning additional values without breaking old call sites, which is a very handy property.

@jiahao
Copy link
Member

jiahao commented Jan 21, 2015

Sorry, misread.

@habemus-papadum
Copy link
Author

Thanks for the feedback -- I should have been more clear that I was seeking comments prior to working on a potential PR (as well as making sure that I wasn't missing some alternate syntax).

@StefanKarpinski
Copy link
Member

No worries – I thought it was clear enough. A PR would be excellent and most appreciated!

@JeffBezanson
Copy link
Member

duplicate of #2626

@StefanKarpinski
Copy link
Member

See? I knew it was a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

7 participants