Conditional
Aqua supports branching, you can:
- Check boolean expression with
if - Recover from error with
tryorotherwise - Return different results with conditional return
Contract
- The second branch of the conditional operator is executed if and only if the first block failed.
- The second branch has no access to the first branch's data.
- A conditional block is considered "executed" if and only if any branch was executed successfully.
- A conditional block is considered "failed" if and only if the second (recovery) branch failed.
Conditional operations
if
The first branch of the conditional operator, executed only if the condition holds.
aquax = trueif x:-- always executedfoo()if x == false:-- never executedbar()if x != false:-- always executedbaz()
aquax = trueif x:-- always executedfoo()if x == false:-- never executedbar()if x != false:-- always executedbaz()
Any expression that produces bool is acceptable as a condition.
if corresponds to match, mismatch extension of π-calculus.
else
The second branch of if, executed only in case the condition does not hold.
aquaif false:foo() -- skippedelse:bar() -- executed
aquaif false:foo() -- skippedelse:bar() -- executed
If you want to set a variable based on condition, see conditional return.
try
Tries to perform operations, swallows produced error (if there's no catch, otherwise executes catch).
aquatry:-- If foo fails with an error, execution will continue-- You should write your logic in a non-blocking fashion:-- If your code below depends on `x`, it may halt as `x` is not resolved.-- See conditional return below for workaroundx <- foo()
aquatry:-- If foo fails with an error, execution will continue-- You should write your logic in a non-blocking fashion:-- If your code below depends on `x`, it may halt as `x` is not resolved.-- See conditional return below for workaroundx <- foo()
catch
Catches the standard error from the try block.
aquatry:foo()catch e:logError(e)
aquatry:foo()catch e:logError(e)
Type of e is:
aquadata LastError:instruction: string -- What AIR instruction failedmsg: string -- Human-readable error messagepeer_id: string -- On what peer the error happened
aquadata LastError:instruction: string -- What AIR instruction failedmsg: string -- Human-readable error messagepeer_id: string -- On what peer the error happened
otherwise
You may add otherwise to provide recovery for any block or expression:
aquax <- foo()otherwise:-- if foo can't be executed, then do bar()y <- bar()
aquax <- foo()otherwise:-- if foo can't be executed, then do bar()y <- bar()
Conditional return
In Aqua, functions may have only one return expression, which is the very last.
So to get the value based on condition, we need to use a writeable collection.
aqua-- result may have 0 or more values of type string and is writeableresultBox: *stringtry:resultBox <- foo()otherwise:resultBox <- bar()-- now result contains only one value, let's extract it!result = resultBox!-- Type of result is string-- Please note that if there were no writes to resultBox,-- the first use of result will halt.-- So you need to be careful about it and ensure that there's always a value.
aqua-- result may have 0 or more values of type string and is writeableresultBox: *stringtry:resultBox <- foo()otherwise:resultBox <- bar()-- now result contains only one value, let's extract it!result = resultBox!-- Type of result is string-- Please note that if there were no writes to resultBox,-- the first use of result will halt.-- So you need to be careful about it and ensure that there's always a value.