0

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
Alex Craft
  • 13,598
  • 11
  • 69
  • 133

1 Answers1

4

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.

xbello
  • 7,223
  • 3
  • 28
  • 41
  • Thanks, although I find this behaviour non-intuitive and confusing. – Alex Craft Aug 19 '20 at 09:04
  • Then use braces. Nim has this philosophy of enabling multiple coding styles (functional, procedural, OOP), but doesn't force any of them. Pick up what you enjoy the most and keep coding :) . – xbello Aug 19 '20 at 15:24