Macros
macro `:=?`(a, b, body, elseBranch): untyped
-
Executes body if a ::= b doesn't give a runtime error, otherwise executes elseBranch. body will be in the same scope as the definition a := b.
Example:
import options let a = some(3) some(n) :=? a: doAssert n == 3 else: doAssert false
Source Edit macro `:=`(a, b): untyped
-
Unpacks b with the given description a.
Example:
(a, b) := (1, 2) c as d := 3 doAssert (a, b, c, d) == (1, 2, 3, 3)
Source Edit macro def(assignments): untyped
-
Goes through each assignment expression in assignments and processes them into definitions.
Example:
def: (a, b) = (1, 2) c as d = 3 doAssert (a, b, c, d) == (1, 2, 3, 3)
Source Edit macro def(assignments, body): untyped
-
Goes through each assignment expression in assignments and processes them into definitions. These definitions are then put into a new scope where body is evaluated.
Example:
var a = 3 def: a = 1 do: doAssert a == 1 doAssert a == 3
Source Edit macro match(val: untyped; branches: varargs[untyped]): untyped
-
Naive pattern matching implementation based on :=?.
Example:
proc fizzbuzz(n: int): string = match (n mod 3, n mod 5): of (0, 0): "FizzBuzz" of (0, _): "Fizz" of (_, 0): "Buzz" else: $n for i in 1..100: echo fizzbuzz(i)
Source Edit macro unpackArgs(args, routine): untyped
-
Injects unpacking assignments into the body of a given routine.
Example:
proc foo(tup: (int, string)) {.unpackArgs: [(a, b): tup].} = doAssert a == b.len let t = (3, "abc") foo(t)
Source Edit
Templates
template `:=?`(a, b): bool
-
Executes (!) a := b and returns false if it gives a runtime error. Otherwise returns true. Note that the executed a := b will not have any affect on the scope of the following statements since it uses a try statement.
Example:
doAssert (a, b) :=? (1, 2) import options let a = none(int) doAssert not (some(n) :=? a)
Source Edit