try - pannous/warp GitHub Wiki

The try keyword, similar to Ruby, extends the simple Java mechanism and even the advanced Swift mechanism: Instead of

        guard let item = inventory[name] else {
            throw VendingMachineError.invalidSelection
        }

Angle gives you

try item = inventory[name] else invalidSelection

When inventory[name] is missing, item is invalidSelection. Not yet (card error-gaps): a field of an Error, like item.count on a failed item, does not raise the Error.

Instead of

        guard item.count > 0 else {
            throw VendingMachineError.outOfStock
        }

In assert, the else text is raised as the error:

assert item.count > 0 else outOfStock

Without try: failed

A function that returns an error value needs no try at all: the caller tests the result.

f(x) := if x < 0 then error "neg" else x
r = f(-1)
if r failed then 1 else 2   # 1

try is for raised errors (raise, 1/0, an index out of range), which propagate instead (Error). A bare try, no else, keeps the raised error as its value, so failed tests it the same way:

r = try 10/0
if r failed then 1 else 2   # 1