{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/15495","repository_url":"https://api.github.com/repos/JuliaLang/julia","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/15495/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/15495/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/15495/events","html_url":"https://github.com/JuliaLang/julia/pull/15495","id":140598485,"number":15495,"title":"@precondition -- like assert but throws ArgumentError","user":{"login":"samoconnor","id":8266283,"avatar_url":"https://avatars.githubusercontent.com/u/8266283?v=3","gravatar_id":"","url":"https://api.github.com/users/samoconnor","html_url":"https://github.com/samoconnor","followers_url":"https://api.github.com/users/samoconnor/followers","following_url":"https://api.github.com/users/samoconnor/following{/other_user}","gists_url":"https://api.github.com/users/samoconnor/gists{/gist_id}","starred_url":"https://api.github.com/users/samoconnor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samoconnor/subscriptions","organizations_url":"https://api.github.com/users/samoconnor/orgs","repos_url":"https://api.github.com/users/samoconnor/repos","events_url":"https://api.github.com/users/samoconnor/events{/privacy}","received_events_url":"https://api.github.com/users/samoconnor/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/error%20handling","name":"error handling","color":"02d7e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":21,"created_at":"2016-03-14T07:36:41Z","updated_at":"2016-03-15T21:42:14Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/JuliaLang/julia/pulls/15495","html_url":"https://github.com/JuliaLang/julia/pull/15495","diff_url":"https://github.com/JuliaLang/julia/pull/15495.diff","patch_url":"https://github.com/JuliaLang/julia/pull/15495.patch"},"body":"`@precondition` is the same as `@assert` but it throws `ArgumentError` and generates an error message of the form `\"ArgumentError: $method_signature requires $condition\"`.\r\n\r\n```julia\r\nfunction split{T}(c::T, n::Int)\r\n    @precondition n > 0\r\n    return SplitIterator{T}(c, n)\r\nend\r\n\r\njulia> split(\"xxx\", 0)\r\nERROR: ArgumentError: split(::ASCIIString, ::Int64) requires n > 0\r\n```\r\n\r\n_Note: this arrises from a request not to use the `@assert` macro for precondition assertions: https://github.com/JuliaLang/julia/pull/15409#commitcomment-16660098 ._"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/197035571","html_url":"https://github.com/JuliaLang/julia/pull/15495#issuecomment-197035571","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/15495","id":197035571,"user":{"login":"eschnett","id":213514,"avatar_url":"https://avatars.githubusercontent.com/u/213514?v=3","gravatar_id":"","url":"https://api.github.com/users/eschnett","html_url":"https://github.com/eschnett","followers_url":"https://api.github.com/users/eschnett/followers","following_url":"https://api.github.com/users/eschnett/following{/other_user}","gists_url":"https://api.github.com/users/eschnett/gists{/gist_id}","starred_url":"https://api.github.com/users/eschnett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eschnett/subscriptions","organizations_url":"https://api.github.com/users/eschnett/orgs","repos_url":"https://api.github.com/users/eschnett/repos","events_url":"https://api.github.com/users/eschnett/events{/privacy}","received_events_url":"https://api.github.com/users/eschnett/received_events","type":"User","site_admin":false},"created_at":"2016-03-15T21:42:14Z","updated_at":"2016-03-15T21:42:14Z","body":"If you have a macro just for the precondition, then it cannot capture the function arguments. If you add a macro before the function (similar to `@inline`), then you can examine the whole function body, and can automatically insert the function argument values into the error message."}}