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

Throw an error when the number of variables on the LHS of an assignment doesn't match the number of elements on the RHS #18312

Open
jrevels opened this issue Aug 31, 2016 · 8 comments
Labels
breaking This change will break code

Comments

@jrevels
Copy link
Member

jrevels commented Aug 31, 2016

Apologies if this is a dup, I thought I saw an issue for this a while ago, but couldn't dig it up.

The current behavior:

julia> x, y = (1, 2, 3)
(1,2,3)

julia> x
1

julia> y
2

@andreasnoack, @vchuravy and I think this behavior's bug-proneness-to-usefulness ratio might be a bit high. We should be consider making it an error. Users could easily (and more clearly) write x, y = take((1,2,3), 2) to accomplish the same thing.

@vchuravy
Copy link
Member

Especially it might be clearer if we introduce _ as a special bitbucket variable. #18251 (comment)

@stevengj
Copy link
Member

The current behavior seems kind of Matlab-inspired, I think. It's really useful for a function like eigs that returns a tuple of a zillion things, but where most of the time you only want two, and you can write λ, X = eigs(...) to discard all of the extra outputs.

As discussed on the mailing list, it also allows you to add outputs to a function that already returns a tuple without breaking most existing code.

@jrevels
Copy link
Member Author

jrevels commented Jan 27, 2017

It's really useful for a function like eigs that returns a tuple of a zillion things, but where most of the time you only want two, and you can write λ, X = eigs(...) to discard all of the extra outputs.

If the caller only wants the first two elements of the return tuple, they can always explicitly write λ, X = take(eigs(...), 2).

@stevengj
Copy link
Member

@jrevels, yes, they can call take, but that's a lot more verbose if the common case is that you want only a subset of the outputs.

@jrevels
Copy link
Member Author

jrevels commented Jan 27, 2017

Not having to explicitly call take only saves a couple of characters, while if you're chasing down a bug as a result of this feature, the investigative cost can be annoyingly high. The OP here was the consensus from a discussion within the MIT group after an overly long bug-chasing session where this behavior was the culprit.

@mbauman
Copy link
Member

mbauman commented Jan 27, 2017

A potential replacement here could be λ, X, _... = eigs(...) — that's #9343 + #2626.

@DNF2
Copy link

DNF2 commented Jan 27, 2017

Is it at least possible to retain λ, X, = eigs(...)? (with the trailing comma.)

@StefanKarpinski
Copy link
Member

We generally allow trailing commas in as many places as possible, so e.g. a, b, = [1,2,3] leaving a == 1 and b == 2. We could, however, allow this a, b, ... = [1,2,3] to explicitly discard the rest of the values. The real utility, imo, of just ignoring additional values is that it allows functions that return tuples of values to return more without breaking code, but maybe that's not as useful as I think in terms of an upgrade path.

@JeffBezanson JeffBezanson added the breaking This change will break code label Mar 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code
Projects
None yet
Development

No branches or pull requests

7 participants