Discard statement
Example:
proc p(x, y: int): int =
result = x + y
discard p(3, 4) # discard the return value of `p`
The discard statement evaluates its expression for side-effects and throws the expression’s resulting value away, and should only be used when ignoring this value is known not to cause problems.
Ignoring the return value of a procedure without using a discard statement is a static error.
The return value can be ignored implicitly if the called proc/iterator has been declared with the discardable pragma:
proc p(x, y: int): int {.discardable.} =
result = x + y
p(3, 4) # now valid
however the discardable pragma does not work on templates as templates substitute the AST in place. For example:
{.push discardable .}
template example(): string = "https://nim-lang.org"
{.pop.}
example()
This template will resolve into “https://nim-lang.org“ which is a string literal and since {.discardable.} doesn’t apply to literals, the compiler will error.
An empty discard statement is often used as a null statement:
proc classify(s: string) =
case s[0]
of SymChars, '_': echo "an identifier"
of '0'..'9': echo "a number"
else: discard