-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add @precondition, like @Assert but thows ArgumentError, e.g.
function split{T}(c::T, n::Int) @require n > 0 return SplitIterator{T}(c, n) end split("xxx", 0) ERROR: ArgumentError: split(::ASCIIString, ::Int64) requires n > 0
- Loading branch information
1 parent
77936d0
commit 412584e
Showing
5 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
|
||
@noinline function precondition_error(msg) | ||
io = IOBuffer() | ||
StackTraces.show_spec_linfo(io, StackTraces.lookup(backtrace()[3])) | ||
throw(ArgumentError("$(takebuf_string(io)) requires $msg")) | ||
end | ||
|
||
|
||
""" | ||
@require precondition [message] | ||
Throw `ArgumentError` if `precondition` is false. | ||
""" | ||
macro require(precondition, msgs...) | ||
msg = isempty(msgs) ? string(precondition) : msgs[1] | ||
:(if ! $(esc(precondition)) precondition_error($msg) end) | ||
end | ||
# FIXME | ||
# Should this have a branch-prediction hint? (same for @assert?) | ||
# http://llvm.org/docs/BranchWeightMetadata.html#built-in-expect-instructions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1436,6 +1436,7 @@ export | |
@noinline, | ||
|
||
@assert, | ||
@require, | ||
@enum, | ||
@label, | ||
@goto, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters