From 21617e899139f7a0a68557c44dcd15d3b2497846 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 16 Jan 2019 17:58:35 -0500 Subject: [PATCH] fix #30499, document behavior of `return` at top level (#30716) also add some missing doc strings and improve others (cherry picked from commit 0bb0332aa41bf02673548cc10fbefa5f9e4d4614) --- base/Enums.jl | 5 +++++ base/docs/basedocs.jl | 41 ++++++++++++++++++++++++++--------------- base/iostream.jl | 6 ++++++ base/strings/io.jl | 17 +++++++++++++++++ base/sysimg.jl | 25 +++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 15 deletions(-) diff --git a/base/Enums.jl b/base/Enums.jl index 87f99e7114789..40bc9a63315dc 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -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 diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index e4cc4d283aaf3..2c2c402d9e81d 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -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) @@ -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" @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 @@ -1246,7 +1257,7 @@ Unsigned """ Bool <: Integer -Boolean type. +Boolean type, containing the values `true` and `false`. """ Bool diff --git a/base/iostream.jl b/base/iostream.jl index dbacbd888bc14..bbb13632747ab 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -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} diff --git a/base/strings/io.jl b/base/strings/io.jl index 7453845211148..757c3888d00be 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -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) diff --git a/base/sysimg.jl b/base/sysimg.jl index 8e0dd138b9cea..d3c2f1e7a833c 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -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