4

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?

GreenSaguaro
  • 2,968
  • 2
  • 22
  • 41

1 Answers1

8

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.

Reimer Behrends
  • 8,600
  • 15
  • 19