Skip to content

Commit 9232de9

Browse files
authored
improve docstring for :: (#41671)
1 parent 799136d commit 9232de9

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

base/docs/basedocs.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,10 +2529,20 @@ UnionAll
25292529
"""
25302530
::
25312531
2532-
With the `::`-operator type annotations are attached to expressions and variables in programs.
2533-
See the manual section on [Type Declarations](@ref).
2532+
The `::` operator either asserts that a value has the given type, or declares that
2533+
a local variable or function return always has the given type.
2534+
2535+
Given `expression::T`, `expression` is first evaluated. If the result is of type
2536+
`T`, the value is simply returned. Otherwise, a [`TypeError`](@ref) is thrown.
2537+
2538+
In local scope, the syntax `local x::T` or `x::T = expression` declares that local variable
2539+
`x` always has type `T`. When a value is assigned to the variable, it will be
2540+
converted to type `T` by calling [`convert`](@ref).
25342541
2535-
Outside of declarations `::` is used to assert that expressions and variables in programs have a given type.
2542+
In a method declaration, the syntax `function f(x)::T` causes any value returned by
2543+
the method to be converted to type `T`.
2544+
2545+
See the manual section on [Type Declarations](@ref).
25362546
25372547
# Examples
25382548
```jldoctest
@@ -2541,6 +2551,13 @@ ERROR: TypeError: typeassert: expected AbstractFloat, got a value of type Int64
25412551
25422552
julia> (1+2)::Int
25432553
3
2554+
2555+
julia> let
2556+
local x::Int
2557+
x = 2.0
2558+
x
2559+
end
2560+
2
25442561
```
25452562
"""
25462563
kw"::"

0 commit comments

Comments
 (0)