Skip to content

Commit

Permalink
Correct some documentation changes
Browse files Browse the repository at this point in the history
Corrections include:
- Remove addition of `eval(::Module, ::Any)` from most stacktraces
- Use doctest groups rather than appending numbers to function names
  • Loading branch information
omus committed Jul 8, 2016
1 parent defb73a commit 921dd30
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 84 deletions.
54 changes: 27 additions & 27 deletions doc/manual/control-flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ This means that new variables defined inside the ``ìf`` clauses can
be used after the ``if`` block, even if they weren't defined before.
So, we could have defined the ``test`` function above as

.. doctest::
.. doctest:: leaky

julia> function test1(x,y)
julia> function test(x,y)
if x < y
relation = "less than"
elseif x == y
Expand All @@ -138,31 +138,31 @@ So, we could have defined the ``test`` function above as
end
println("x is ", relation, " y.")
end
test1 (generic function with 1 method)
test (generic function with 1 method)

The variable ``relation`` is declared inside the ``if`` block, but used
outside. However, when depending on this behavior, make sure all possible
code paths define a value for the variable. The following change to
the above function results in a runtime error

.. doctest::
.. doctest:: bad-leaky

julia> function test2(x,y)
julia> function test(x,y)
if x < y
relation = "less than"
elseif x == y
relation = "equal to"
end
println("x is ", relation, " y.")
end
test2 (generic function with 1 method)
test (generic function with 1 method)

julia> test2(1,2)
julia> test(1,2)
x is less than y.

julia> test2(2,1)
julia> test(2,1)
ERROR: UndefVarError: relation not defined
in test2(::Int64, ::Int64) at ./none:7
in test(::Int64, ::Int64) at ./none:7
...

``if`` blocks also return a value, which may seem unintuitive to users
Expand Down Expand Up @@ -236,19 +236,19 @@ evaluates to the string ``"not less than"``. The original three-way
example requires chaining multiple uses of the ternary operator
together:

.. doctest::
.. doctest:: ternary-operator

julia> test4(x, y) = println(x < y ? "x is less than y" :
julia> test(x, y) = println(x < y ? "x is less than y" :
x > y ? "x is greater than y" : "x is equal to y")
test4 (generic function with 1 method)
test (generic function with 1 method)

julia> test4(1, 2)
julia> test(1, 2)
x is less than y

julia> test4(2, 1)
julia> test(2, 1)
x is greater than y

julia> test4(1, 1)
julia> test(1, 1)
x is equal to y

To facilitate chaining, the operator associates from right to left.
Expand Down Expand Up @@ -682,23 +682,23 @@ Exceptions can be created explicitly with :func:`throw`. For example, a function
defined only for nonnegative numbers could be written to :func:`throw` a :exc:`DomainError`
if the argument is negative:

.. doctest::
.. doctest:: domain-error

julia> g(x) = x>=0 ? exp(-x) : throw(DomainError())
g (generic function with 1 method)
julia> f(x) = x>=0 ? exp(-x) : throw(DomainError())
f (generic function with 1 method)

julia> g(1)
julia> f(1)
0.36787944117144233

julia> g(-1)
julia> f(-1)
ERROR: DomainError:
in g(::Int64) at ./none:1
in f(::Int64) at ./none:1
...

Note that :exc:`DomainError` without parentheses is not an exception, but a type of
exception. It needs to be called to obtain an :exc:`Exception` object:

.. doctest::
.. doctest:: throw-function

julia> typeof(DomainError()) <: Exception
true
Expand Down Expand Up @@ -806,19 +806,19 @@ example, a customized square root function can be written to automatically
call either the real or complex square root method on demand using
:exc:`Exception`\ s :

.. doctest::
.. doctest:: try-catch

julia> h(x) = try
julia> f(x) = try
sqrt(x)
catch
sqrt(complex(x, 0))
end
h (generic function with 1 method)
f (generic function with 1 method)

julia> h(1)
julia> f(1)
1.0

julia> h(-1)
julia> f(-1)
0.0 + 1.0im

It is important to note that in real code computing this function, one would
Expand Down
22 changes: 6 additions & 16 deletions doc/manual/integers-and-floating-point-numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -412,24 +412,14 @@ types:

.. doctest::

julia> typemin(Float16)
-Inf16
julia> (typemin(Float16),typemax(Float16))
(-Inf16,Inf16)

julia> typemax(Float16)
Inf16

julia> typemin(Float32)
-Inf32

julia> typemax(Float32)
Inf32

julia> typemin(Float64)
-Inf

julia> typemax(Float64)
Inf
julia> (typemin(Float32),typemax(Float32))
(-Inf32,Inf32)

julia> (typemin(Float64),typemax(Float64))
(-Inf,Inf)

Machine epsilon
~~~~~~~~~~~~~~~
Expand Down
12 changes: 8 additions & 4 deletions doc/manual/metaprogramming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,15 @@ Basics

Here is an extraordinarily simple macro:

.. doctest::
.. testcode::

macro sayhello()
return :( println("Hello, world!") )
end

.. testoutput::
:hide:

julia> macro sayhello()
return :( println("Hello, world!") )
end
@sayhello (macro with 1 method)

Macros have a dedicated character in Julia's syntax: the ``@`` (at-sign),
Expand Down
14 changes: 7 additions & 7 deletions doc/manual/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,21 @@ more specific than the other. In such cases, Julia raises a ``MethodError``
rather than arbitrarily picking a method. You can avoid method ambiguities
by specifying an appropriate method for the intersection case:

.. doctest::
.. doctest:: unambiguous

julia> h(x::Float64, y::Float64) = 2x + 2y;
julia> g(x::Float64, y::Float64) = 2x + 2y;

julia> h(x::Float64, y) = 2x + y;
julia> g(x::Float64, y) = 2x + y;

julia> h(x, y::Float64) = x + 2y;
julia> g(x, y::Float64) = x + 2y;

julia> h(2.0, 3)
julia> g(2.0, 3)
7.0

julia> h(2, 3.0)
julia> g(2, 3.0)
8.0

julia> h(2.0, 3.0)
julia> g(2.0, 3.0)
10.0

It is recommended that the disambiguating method be defined first,
Expand Down
55 changes: 26 additions & 29 deletions doc/manual/stacktraces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ alias :obj:`StackTrace` can be used in place of ``Vector{StackFrame}``. (Example
in child() at none:1
in parent() at none:1
in grandparent() at none:1
in eval(::Module, ::Any) at boot.jl:231
...

Note that when calling :func:`stacktrace` you'll typically see a frame with
Expand Down Expand Up @@ -113,21 +112,21 @@ Error handling
While having easy access to information about the current state of the callstack can be
helpful in many places, the most obvious application is in error handling and debugging.

.. doctest::
.. doctest:: error-handling

julia> @noinline bad_function() = undeclared_variable
bad_function (generic function with 1 method)

julia> @noinline example1() = try
julia> @noinline example() = try
bad_function()
catch
stacktrace()
end
example1 (generic function with 1 method)
example (generic function with 1 method)

julia> example1()
julia> example()
6-element Array{StackFrame,1}:
in example1() at none:4
in example() at none:4
in eval(::Module, ::Any) at boot.jl:231
...

Expand All @@ -142,52 +141,50 @@ This can be remedied by calling :func:`catch_stacktrace` instead of :func:`stack
Instead of returning callstack information for the current context, :func:`catch_stacktrace`
returns stack information for the context of the most recent exception:

.. doctest::
.. doctest:: catch-stacktrace

julia> @noinline bad_function1() = undeclared_variable
bad_function1 (generic function with 1 method)
julia> @noinline bad_function() = undeclared_variable
bad_function (generic function with 1 method)

julia> @noinline example2() = try
bad_function1()
julia> @noinline example() = try
bad_function()
catch
catch_stacktrace()
end
example2 (generic function with 1 method)
example (generic function with 1 method)

julia> example2()
julia> example()
7-element Array{StackFrame,1}:
in bad_function1() at none:1
in example2() at none:2
in eval(::Module, ::Any) at boot.jl:231
in bad_function() at none:1
in example() at none:2
...

Notice that the stack trace now indicates the appropriate line number and the missing frame.

.. doctest::
.. doctest:: catch-stacktrace-demo

julia> @noinline child1() = error("Whoops!")
child1 (generic function with 1 method)
julia> @noinline child() = error("Whoops!")
child (generic function with 1 method)

julia> @noinline parent1() = child1()
parent1 (generic function with 1 method)
julia> @noinline parent() = child()
parent (generic function with 1 method)

julia> @noinline function grandparent1()
julia> @noinline function grandparent()
try
parent1()
parent()
catch err
println("ERROR: ", err.msg)
catch_stacktrace()
end
end
grandparent1 (generic function with 1 method)
grandparent (generic function with 1 method)

julia> grandparent1()
julia> grandparent()
ERROR: Whoops!
8-element Array{StackFrame,1}:
in child1() at none:1
in parent1() at none:1
in grandparent1() at none:3
in eval(::Module, ::Any) at boot.jl:231
in child() at none:1
in parent() at none:1
in grandparent() at none:3
...

Comparison with :func:`backtrace`
Expand Down
3 changes: 2 additions & 1 deletion doc/manual/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,8 @@ You can safely access the value of a :obj:`Nullable` object using :func:`get`:

julia> get(Nullable{Float64}())
ERROR: NullException()
in get(::Nullable{Float64}) at ./nullable.jl:62...
in get(::Nullable{Float64}) at ./nullable.jl:62
...

julia> get(Nullable(1.0))
1.0
Expand Down

0 comments on commit 921dd30

Please sign in to comment.