Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between void proc and void proc with discard

Tags:

nim-lang

Given the following two procs:

proc firstOne(): void =
    echo "X"

proc secondOne(): void =
    echo "X"
    discard

What functional difference, if any, is there between them? And if they are the same, what is the purpose of discard if void type discards the result?

like image 311
GreenSaguaro Avatar asked Sep 08 '25 01:09

GreenSaguaro


1 Answers

The discard in the second procedure is superfluous. A discard without an argument is simply a no-op. It is normally used (like pass in Python) where the language syntax requires a statement, but where you don't want to do anything. An example would be an empty procedure:

proc doNothing() =
  discard

You can still add discard even where it is not syntactically necessary because as a no-op it doesn't do anything.

This is different from discard with an argument, whose purpose is to call a function for its side effects and ignore the result.

like image 158
Reimer Behrends Avatar answered Sep 11 '25 00:09

Reimer Behrends