Skip to content

Commit

Permalink
doc: fix the assert example to be consistent with doctest output [av …
Browse files Browse the repository at this point in the history
…skip]
  • Loading branch information
gloine committed Nov 9, 2015
1 parent e10e039 commit 8f21bd3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions doc/manual/metaprogramming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Building an advanced macro
Here is a simplified definition of Julia's :obj:`@assert` macro::

macro assert(ex)
return :($ex ? nothing : error("Assertion failed: ", $(string(ex))))
return :( $ex ? nothing : throw(AssertionError($(string(ex)))) )
end

This macro can be used like this:
Expand All @@ -545,13 +545,13 @@ This macro can be used like this:
julia> @assert 1==1.0

julia> @assert 1==0
ERROR: Assertion failed: 1 == 0
ERROR: AssertionError: 1 == 0

In place of the written syntax, the macro call is expanded at parse time to
its returned result. This is equivalent to writing::

1==1.0 ? nothing : error("Assertion failed: ", "1==1.0")
1==0 ? nothing : error("Assertion failed: ", "1==0")
1==1.0 ? nothing : throw(AssertionError("1==1.0"))
1==0 ? nothing : throw(AssertionError("1==0"))

That is, in the first call, the expression ``:(1==1.0)`` is spliced into
the test condition slot, while the value of ``string(:(1==1.0))`` is
Expand All @@ -572,8 +572,8 @@ ellipses following the last argument::

macro assert(ex, msgs...)
msg_body = isempty(msgs) ? ex : msgs[1]
msg = string("assertion failed: ", msg_body)
return :($ex ? nothing : error($msg))
msg = string(msg_body)
return :($ex ? nothing : throw(AssertionError($msg)))
end

Now :obj:`@assert` has two modes of operation, depending upon the number of
Expand Down

0 comments on commit 8f21bd3

Please sign in to comment.