Skip to content

Commit 21617e8

Browse files
JeffBezansonKristofferC
authored andcommitted
fix #30499, document behavior of return at top level (#30716)
also add some missing doc strings and improve others (cherry picked from commit 0bb0332)
1 parent 6bc6e9a commit 21617e8

File tree

5 files changed

+79
-15
lines changed

5 files changed

+79
-15
lines changed

base/Enums.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export Enum, @enum
77

88
function basetype end
99

10+
"""
11+
Enum{T<:Integer}
12+
13+
The abstract supertype of all enumerated types defined with [`@enum`](@ref).
14+
"""
1015
abstract type Enum{T<:Integer} end
1116

1217
(::Type{T})(x::Enum{T2}) where {T<:Integer,T2<:Integer} = T(bitcast(T2, x))::T

base/docs/basedocs.jl

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ kw"function"
345345
"""
346346
return
347347
348-
`return` can be used in function bodies to exit early and return a given value, e.g.
348+
`return x` causes the enclosing function to exit early, passing the given value `x`
349+
back to its caller. `return` by itself with no value is equivalent to `return nothing`
350+
(see [`nothing`](@ref)).
349351
350352
```julia
351353
function compare(a, b)
@@ -371,12 +373,15 @@ function test2(xs)
371373
end
372374
end
373375
```
374-
In the first example, the return breaks out of its enclosing function as soon as it hits
376+
In the first example, the return breaks out of `test1` as soon as it hits
375377
an even number, so `test1([5,6,7])` returns `12`.
376378
377379
You might expect the second example to behave the same way, but in fact the `return`
378380
there only breaks out of the *inner* function (inside the `do` block) and gives a value
379381
back to `map`. `test2([5,6,7])` then returns `[5,12,7]`.
382+
383+
When used in a top-level expression (i.e. outside any function), `return` causes
384+
the entire current top-level expression to terminate early.
380385
"""
381386
kw"return"
382387

@@ -407,7 +412,7 @@ kw"if", kw"elseif", kw"else"
407412
"""
408413
for
409414
410-
`for` loops repeatedly evaluate the body of the loop by
415+
`for` loops repeatedly evaluate a block of statements while
411416
iterating over a sequence of values.
412417
413418
# Examples
@@ -425,8 +430,8 @@ kw"for"
425430
"""
426431
while
427432
428-
`while` loops repeatedly evaluate a conditional expression, and continues evaluating the
429-
body of the while loop so long as the expression remains `true`. If the condition
433+
`while` loops repeatedly evaluate a conditional expression, and continue evaluating the
434+
body of the while loop as long as the expression remains true. If the condition
430435
expression is false when the while loop is first reached, the body is never evaluated.
431436
432437
# Examples
@@ -473,19 +478,23 @@ kw"end"
473478
"""
474479
try/catch
475480
476-
A `try`/`catch` statement allows for `Exception`s to be tested for. For example, a
477-
customized square root function can be written to automatically call either the real or
478-
complex square root method on demand using `Exception`s:
481+
A `try`/`catch` statement allows intercepting errors (exceptions) thrown
482+
by [`throw`](@ref) so that program execution can continue.
483+
For example, the following code attempts to write a file, but warns the user
484+
and proceeds instead of terminating execution if the file cannot be written:
479485
480486
```julia
481-
f(x) = try
482-
sqrt(x)
487+
try
488+
open("/danger", "w") do f
489+
println(f, "Hello")
490+
end
483491
catch
484-
sqrt(complex(x, 0))
492+
@warn "Could not write file."
485493
end
486494
```
487495
488-
`try`/`catch` statements also allow the `Exception` to be saved in a variable, e.g. `catch y`.
496+
The syntax `catch e` (where `e` is any variable) assigns the thrown
497+
exception object to the given variable within the `catch` block.
489498
490499
The power of the `try`/`catch` construct lies in the ability to unwind a deeply
491500
nested computation immediately to a much higher level in the stack of calling functions.
@@ -561,7 +570,9 @@ kw"continue"
561570
"""
562571
do
563572
564-
Create an anonymous function. For example:
573+
Create an anonymous function and pass it as the first argument to
574+
a function call.
575+
For example:
565576
566577
```julia
567578
map(1:10) do x
@@ -842,7 +853,7 @@ nothing
842853
"""
843854
Core.TypeofBottom
844855
845-
The singleton type containing only the value `Union{}`.
856+
The singleton type containing only the value `Union{}` (which represents the empty type).
846857
"""
847858
Core.TypeofBottom
848859

@@ -1246,7 +1257,7 @@ Unsigned
12461257
"""
12471258
Bool <: Integer
12481259
1249-
Boolean type.
1260+
Boolean type, containing the values `true` and `false`.
12501261
"""
12511262
Bool
12521263

base/iostream.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
const sizeof_ios_t = Int(ccall(:jl_sizeof_ios_t, Cint, ()))
66

7+
"""
8+
IOStream
9+
10+
A buffered IO stream wrapping an OS file descriptor.
11+
Mostly used to represent files returned by [`open`](@ref).
12+
"""
713
mutable struct IOStream <: IO
814
handle::Ptr{Cvoid}
915
ios::Array{UInt8,1}

base/strings/io.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,24 @@ function unescape_string(io, s::AbstractString)
440440
end
441441
unescape_string(s::AbstractString) = sprint(unescape_string, s, sizehint=lastindex(s))
442442

443+
"""
444+
@b_str
445+
446+
Create an immutable byte (`UInt8`) vector using string syntax.
443447
448+
# Examples
449+
```jldoctest
450+
julia> v = b"12\\x01\\x02"
451+
4-element Base.CodeUnits{UInt8,String}:
452+
0x31
453+
0x32
454+
0x01
455+
0x02
456+
457+
julia> v[2]
458+
0x32
459+
```
460+
"""
444461
macro b_str(s)
445462
v = codeunits(unescape_string(s))
446463
QuoteNode(v)

base/sysimg.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,32 @@ include("abstractarraymath.jl")
186186
include("arraymath.jl")
187187

188188
# define MIME"foo/bar" early so that we can overload 3-arg show
189+
"""
190+
MIME
191+
192+
A type representing a standard internet data format. "MIME" stands for
193+
"Multipurpose Internet Mail Extensions", since the standard was originally
194+
used to describe multimedia attachments to email messages.
195+
196+
A `MIME` object can be passed as the second argument to [`show`](@ref) to
197+
request output in that format.
198+
199+
# Examples
200+
```jldoctest
201+
julia> show(stdout, MIME("text/plain"), "hi")
202+
"hi"
203+
```
204+
"""
189205
struct MIME{mime} end
206+
207+
"""
208+
@MIME_str
209+
210+
A convenience macro for writing [`MIME`](@ref) types, typically used when
211+
adding methods to `show`.
212+
For example the syntax `show(io::IO, ::MIME"text/html", x::MyType) = ...`
213+
could be used to define how to write an HTML representation of `MyType`.
214+
"""
190215
macro MIME_str(s)
191216
:(MIME{$(Expr(:quote, Symbol(s)))})
192217
end

0 commit comments

Comments
 (0)