Skip to content

Commit

Permalink
fix #30499, document behavior of return at top level (#30716)
Browse files Browse the repository at this point in the history
also add some missing doc strings and improve others

(cherry picked from commit 0bb0332)
  • Loading branch information
JeffBezanson authored and KristofferC committed Feb 11, 2019
1 parent 6bc6e9a commit 21617e8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
5 changes: 5 additions & 0 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export Enum, @enum

function basetype end

"""
Enum{T<:Integer}
The abstract supertype of all enumerated types defined with [`@enum`](@ref).
"""
abstract type Enum{T<:Integer} end

(::Type{T})(x::Enum{T2}) where {T<:Integer,T2<:Integer} = T(bitcast(T2, x))::T
Expand Down
41 changes: 26 additions & 15 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ kw"function"
"""
return
`return` can be used in function bodies to exit early and return a given value, e.g.
`return x` causes the enclosing function to exit early, passing the given value `x`
back to its caller. `return` by itself with no value is equivalent to `return nothing`
(see [`nothing`](@ref)).
```julia
function compare(a, b)
Expand All @@ -371,12 +373,15 @@ function test2(xs)
end
end
```
In the first example, the return breaks out of its enclosing function as soon as it hits
In the first example, the return breaks out of `test1` as soon as it hits
an even number, so `test1([5,6,7])` returns `12`.
You might expect the second example to behave the same way, but in fact the `return`
there only breaks out of the *inner* function (inside the `do` block) and gives a value
back to `map`. `test2([5,6,7])` then returns `[5,12,7]`.
When used in a top-level expression (i.e. outside any function), `return` causes
the entire current top-level expression to terminate early.
"""
kw"return"

Expand Down Expand Up @@ -407,7 +412,7 @@ kw"if", kw"elseif", kw"else"
"""
for
`for` loops repeatedly evaluate the body of the loop by
`for` loops repeatedly evaluate a block of statements while
iterating over a sequence of values.
# Examples
Expand All @@ -425,8 +430,8 @@ kw"for"
"""
while
`while` loops repeatedly evaluate a conditional expression, and continues evaluating the
body of the while loop so long as the expression remains `true`. If the condition
`while` loops repeatedly evaluate a conditional expression, and continue evaluating the
body of the while loop as long as the expression remains true. If the condition
expression is false when the while loop is first reached, the body is never evaluated.
# Examples
Expand Down Expand Up @@ -473,19 +478,23 @@ kw"end"
"""
try/catch
A `try`/`catch` statement allows for `Exception`s to be tested for. For example, a
customized square root function can be written to automatically call either the real or
complex square root method on demand using `Exception`s:
A `try`/`catch` statement allows intercepting errors (exceptions) thrown
by [`throw`](@ref) so that program execution can continue.
For example, the following code attempts to write a file, but warns the user
and proceeds instead of terminating execution if the file cannot be written:
```julia
f(x) = try
sqrt(x)
try
open("/danger", "w") do f
println(f, "Hello")
end
catch
sqrt(complex(x, 0))
@warn "Could not write file."
end
```
`try`/`catch` statements also allow the `Exception` to be saved in a variable, e.g. `catch y`.
The syntax `catch e` (where `e` is any variable) assigns the thrown
exception object to the given variable within the `catch` block.
The power of the `try`/`catch` construct lies in the ability to unwind a deeply
nested computation immediately to a much higher level in the stack of calling functions.
Expand Down Expand Up @@ -561,7 +570,9 @@ kw"continue"
"""
do
Create an anonymous function. For example:
Create an anonymous function and pass it as the first argument to
a function call.
For example:
```julia
map(1:10) do x
Expand Down Expand Up @@ -842,7 +853,7 @@ nothing
"""
Core.TypeofBottom
The singleton type containing only the value `Union{}`.
The singleton type containing only the value `Union{}` (which represents the empty type).
"""
Core.TypeofBottom

Expand Down Expand Up @@ -1246,7 +1257,7 @@ Unsigned
"""
Bool <: Integer
Boolean type.
Boolean type, containing the values `true` and `false`.
"""
Bool

Expand Down
6 changes: 6 additions & 0 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

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

"""
IOStream
A buffered IO stream wrapping an OS file descriptor.
Mostly used to represent files returned by [`open`](@ref).
"""
mutable struct IOStream <: IO
handle::Ptr{Cvoid}
ios::Array{UInt8,1}
Expand Down
17 changes: 17 additions & 0 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,24 @@ function unescape_string(io, s::AbstractString)
end
unescape_string(s::AbstractString) = sprint(unescape_string, s, sizehint=lastindex(s))

"""
@b_str
Create an immutable byte (`UInt8`) vector using string syntax.
# Examples
```jldoctest
julia> v = b"12\\x01\\x02"
4-element Base.CodeUnits{UInt8,String}:
0x31
0x32
0x01
0x02
julia> v[2]
0x32
```
"""
macro b_str(s)
v = codeunits(unescape_string(s))
QuoteNode(v)
Expand Down
25 changes: 25 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,32 @@ include("abstractarraymath.jl")
include("arraymath.jl")

# define MIME"foo/bar" early so that we can overload 3-arg show
"""
MIME
A type representing a standard internet data format. "MIME" stands for
"Multipurpose Internet Mail Extensions", since the standard was originally
used to describe multimedia attachments to email messages.
A `MIME` object can be passed as the second argument to [`show`](@ref) to
request output in that format.
# Examples
```jldoctest
julia> show(stdout, MIME("text/plain"), "hi")
"hi"
```
"""
struct MIME{mime} end

"""
@MIME_str
A convenience macro for writing [`MIME`](@ref) types, typically used when
adding methods to `show`.
For example the syntax `show(io::IO, ::MIME"text/html", x::MyType) = ...`
could be used to define how to write an HTML representation of `MyType`.
"""
macro MIME_str(s)
:(MIME{$(Expr(:quote, Symbol(s)))})
end
Expand Down

0 comments on commit 21617e8

Please sign in to comment.