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

Actual behaviour of unary : is poorly documented #43054

Closed
jakobnissen opened this issue Nov 12, 2021 · 2 comments
Closed

Actual behaviour of unary : is poorly documented #43054

jakobnissen opened this issue Nov 12, 2021 · 2 comments
Labels
docs This change adds or pertains to documentation

Comments

@jakobnissen
Copy link
Contributor

jakobnissen commented Nov 12, 2021

So, what does unary : do? The docstring of : states

: is also used in indexing to select whole dimensions and for Symbol literals, as in e.g. :hello.

. Julia's manual mentions a second use, not mentioned in the docstring:

 The : character, followed by paired parentheses around a single statement of Julia code, produces an Expr object based on the enclosed code.

Perhaps more bizarrely, at least to me, sometimes : does neither - in fact it does nothing at all:

julia> :1
1

And when exaclty is does nothing at all is completely inscrutable, at least to me. Is it perhaps only with literals?
Nope:

julia> :1111111111111111111111
:(1111111111111111111111)

What is even a literal? Julia's manual talks about e.g. array literals, but

julia> :([])
:([])

If the answer is: "The : operator does nothing if the parser recognizes the following string being a literal value, without needing to call any macros or such", then that looks like a leaky abstraction to me - the precise behaviour of what the parser knows at certain times impact the value returned in the REPL when the user types something in.

This discussion is not just academic, it was sparked by a nasty bug encountered by a person used to coding in Python, who used the index [:2] instead of [1:2], and was very surprised to find that :2 evaluated to just 2.

Can this behaviour be documented? It's all very confusing to me.

Edit: After a long and patient explanation by @Seelengrab of what : does, perhaps it would be much clearer if it said something along the lines of ":x is equivalent to Meta.parse("x"), and can return an Expr, a Symbol or a literal value."

@JeffBezanson
Copy link
Member

":x is equivalent to Meta.parse("x"), and can return an Expr, a Symbol or a literal value."

👍
Yes this is correct; it is just code quoting, returning the AST of the following expression. I wouldn't say it's equivalent to parse because interpolation might do something slightly different, but it is very similar.

@JeffBezanson JeffBezanson added the docs This change adds or pertains to documentation label Nov 12, 2021
jakobnissen added a commit to jakobnissen/julia that referenced this issue May 23, 2022
Unary `:` was poorly documented previously. The previous docs stated it would
return a Symbol, but in fact also Expr objects and literal values can be
returned from code quoting.

See JuliaLang#43054
aviatesk added a commit that referenced this issue Jun 1, 2022
Unary `:` was poorly documented previously. The previous docs stated it would
return a Symbol, but in fact also Expr objects and literal values can be
returned from code quoting.

See #43054

Co-authored-by: Shuhei Kadowaki <[email protected]>
Co-authored-by: Valentin Churavy <[email protected]>
@jakobnissen
Copy link
Contributor Author

Solved by #45426

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
Projects
None yet
Development

No branches or pull requests

2 participants