Search code examples
pragmanim-lang

What's the difference between returning void and {.noreturn.}?


In Nim, the noReturn pragma marks a proc that never returns.

How is that different than a function that returns void?


Solution

  • Returning void means the function returns nothing:

    proc saySomething(): void =
      echo "something"
    

    The empty brackets as well as the : void are optional:

    proc saySomething =
      echo "something"
    

    Annotating a function with noReturn means the function will not return at all:

    proc killTheProgram {.noReturn.} =
      quit(0)
    
    proc raiseSomething {.noReturn.} =
      raise newException(ValueError, "Something")