nim-lang

Why can't I call Nim proc without braces?


Nim support proc calling expression without braces, but when I use named arguments it complains, why?

proc doc(text: string) {.discardable.} = echo text
doc "doc1"
doc(text = "doc1")
doc text = "doc1" # <== Error here

Solution

  • The complain is Error: undeclared identifier: 'text', because you're calling the doc proc with a value that is undeclared. This works:

    proc doc(text: string) = echo text
    
    let text = "doc1"
    doc text
    

    The line doc text = "doc1" tells the program to 1) call the procedure doc with the variable text as first argument and 2) assign "doc1" to whatever that procedure returned. And so you'll find the error Error: 'doc text' cannot be assigned to.