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

Document assignment syntax #8191

Closed
eschnett opened this issue Aug 30, 2014 · 5 comments
Closed

Document assignment syntax #8191

eschnett opened this issue Aug 30, 2014 · 5 comments
Labels
docs This change adds or pertains to documentation good first issue Indicates a good issue for first-time contributors to Julia

Comments

@eschnett
Copy link
Contributor

I believe the documentation does not describe the syntax of the assignment operator. From examples, I know that e.g.

a,b = 1,2

is possible. However, I don't know whether this is a special syntax for assignment, or whether LHS and RHS are implicitly interpreted as tuples.

I found out the hard way that writing

(a,b) = x

interprets x as iterable, leading to a very strange error about a method "start" when x returns a value that is not iterable. Apparently, assignment to a tuple can automatically convert e.g. from lists -- naively, I would have expected that I have to call a function for this.

I also found out that

(a,) = (1,2)

is possible, and silently discards the additional value. I would have expected a warning in this case.

Finally, I somehow expected that "_" (using an underscore as variable name) is a placeholder that I can use to discard values, as in

(a,_) = (1,2)

However, this instead creates a local variable called "_".

Since I did not expect these behaviours, others may be surprised as well. It would be good to describe them in the documentation.

@stevengj stevengj added the doc label Aug 31, 2014
@JeffBezanson
Copy link
Member

You're right, the full behavior of multiple assignment from iterators is not documented. That should be remedied.

It's slightly open to interpretation whether "reading the whole manual" or "just trying it" counts as "the hard way" to find something out :)

@elextr
Copy link

elextr commented Sep 2, 2014

"Just trying it" is often the way I find the "wrong way"™ to do it :)

@pao
Copy link
Member

pao commented Sep 2, 2014

However, this instead creates a local variable called "_".

I had discussed this previously with @StefanKarpinski--to summarize, there's not really a reason not to make the assignment, and _ is a legal identifier, so that's what it does. By convention, you would not proceed to use _ on the RHS of an expression.

@kshyatt kshyatt added the good first issue Indicates a good issue for first-time contributors to Julia label Jan 25, 2017
@Moelf
Copy link
Contributor

Moelf commented May 7, 2019

Do we know where would be a good place / keywords to document this?

@laborg
Copy link
Contributor

laborg commented Feb 2, 2022

This has been documented in #30745

@laborg laborg closed this as completed Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation good first issue Indicates a good issue for first-time contributors to Julia
Projects
None yet
Development

No branches or pull requests

8 participants