Skip to content

Commit

Permalink
Improve documentation of unary : (#45426)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people committed Jun 1, 2022
1 parent 0fce312 commit 8373146
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 27 additions & 0 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,33 @@ Expr
"""
Expr

"""
(:)(expr)
`:expr` quotes the expression `expr`, returning the abstract syntax tree (AST) of `expr`.
The AST may be of type `Expr`, `Symbol`, or a literal value.
Which of these three types are returned for any given expression is an
implementation detail.
See also: [`Expr`](@ref), [`Symbol`](@ref), [`Meta.parse`](@ref)
# Examples
```jldoctest
julia> expr = :(a = b + 2*x)
:(a = b + 2x)
julia> sym = :some_identifier
:some_identifier
julia> value = :0xff
0xff
julia> typeof((expr, sym, value))
Tuple{Expr, Symbol, UInt8}
```
"""
(:)

"""
\$
Expand Down
3 changes: 1 addition & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ _colon(::Any, ::Any, start::T, step, stop::T) where {T} =
Range operator. `a:b` constructs a range from `a` to `b` with a step size of 1 (a [`UnitRange`](@ref))
, and `a:s:b` is similar but uses a step size of `s` (a [`StepRange`](@ref)).
`:` is also used in indexing to select whole dimensions
and for [`Symbol`](@ref) literals, as in e.g. `:hello`.
`:` is also used in indexing to select whole dimensions, e.g. in `A[:, 1]`.
"""
(:)(start::T, step, stop::T) where {T} = _colon(start, step, stop)
(:)(start::T, step, stop::T) where {T<:Real} = _colon(start, step, stop)
Expand Down

0 comments on commit 8373146

Please sign in to comment.