-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
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."