0

How do we have Nim function default parameter/argument for mutable ( r/w ) argument the simplest i.e. usually in boolean? e.g. illustration:

proc foo( m:int; n :var int) =    # <- how the correct one
  if n :
   echo 7+m+n
  else :
   echo m 

foo 7    # <- as it's demanded
itil memek cantik
  • 1,167
  • 2
  • 11
  • Actually there is a trick available, you can use cast to pass a default with nil address, and then test in the proc if address of var parameter is nil. But that trick is more for C wrappers, it is ugly and may not work in Nim 2.0 any more. See https://forum.nim-lang.org/t/7117#44859 – Salewski Mar 04 '22 at 07:19

1 Answers1

0

In this case you want to use function overloading instead of default params. If you use a literal as the value for a default var param, it's not clear what should happen if you try and change it.

proc foo(m: int) =
  echo m

proc foo(m: int, n: var int) =
  echo 7 + m + n

foo(7)  # prints 7

var n = 3
foo(7, n)  # prints 17
huantian
  • 111
  • 3