ResultTypes.jl icon indicating copy to clipboard operation
ResultTypes.jl copied to clipboard

Add macro for throwing exceptions

Open MarkusLohmayer opened this issue 3 years ago • 0 comments

I think this is really a great package! Although I prefer to use ResultTypes over plain Julia exceptions in most situations, in some cases I still want to throw exceptions rather than propagate the Result using @try. Therefore I added another macro called @throw.

Example:

foo(i::Int)::Result{Int,ArgumentError} = i ≥ 0 ? i : ArgumentError("negative `i`")
result = @throw(foo(1))
@assert result == 1
result = @throw(foo(-1)) # throws `ArgumentError`

Maybe you agree that this would be a convenient addition to your package! Please let me know if I can improve upon my proposed contribution. Thanks.

MarkusLohmayer avatar Jan 03 '22 10:01 MarkusLohmayer